advent-2022-python/advent/days/day06/solution.py
2022-12-06 06:27:39 +01:00

30 lines
733 B
Python

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")