day06 finished

This commit is contained in:
Ruediger Ludwig 2022-12-06 06:27:39 +01:00
parent 743c1034b4
commit fd1467d11e
6 changed files with 64 additions and 0 deletions

View file

@ -0,0 +1,30 @@
from __future__ import annotations
from itertools import product
from typing import Iterator
day_num = 6
def part1(lines: Iterator[str]) -> int:
return marker(next(lines), 4)
def part2(lines: Iterator[str]) -> int:
return marker(next(lines), 14)
def marker(line: str, length: int) -> int:
"""
Returns the position just after a marker. A marker has [length] non repeated characters.
Raises Exception if no marker was found
"""
for pos in range(length, len(line)):
identical = 0
for a, b in product(line[pos - length:pos], repeat=2):
if a == b:
identical += 1
if identical == length:
return pos
raise Exception("No marker found")