From 7de54241f1f0dc29fb380b7a234834195434e310 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 6 Dec 2022 07:49:16 +0100 Subject: [PATCH] day 6 --- src/aoc/aoc.py | 4 +++- src/day6.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/day6.py diff --git a/src/aoc/aoc.py b/src/aoc/aoc.py index 2f80946..6352b59 100644 --- a/src/aoc/aoc.py +++ b/src/aoc/aoc.py @@ -23,8 +23,10 @@ def get_input(year: int, day: int) -> str: original code from https://github.com/anthonywritescode/aoc2022/blob/main/support/support.py """ file = f'input/{year}/input_{day}.txt' - if not os.path.exists(file): + if not os.path.exists(os.path.dirname(file)): os.makedirs(os.path.dirname(file)) + + if not os.path.exists(file): url = f'https://adventofcode.com/{year}/day/{day}/input' req = urllib.request.Request(url, headers=_get_cookie_headers()) txt = urllib.request.urlopen(req).read().decode() diff --git a/src/day6.py b/src/day6.py new file mode 100644 index 0000000..60dc655 --- /dev/null +++ b/src/day6.py @@ -0,0 +1,64 @@ +from typing import Optional + +from cpl_core.console import Console +from cpl_core.utils import String +from cpl_query.enumerable import Enumerable +from cpl_query.extension import List +from cpl_core.pipes import * + +from aoc.aoc import get_input + +# global vars +day = 6 +aoc_input = get_input(2022, day) +# aoc_input = "mjqjpqmgbljsphdztnvjfqwrcgsmlb" + + +class Marker: + + def __init__(self, part2=False): + self._chars = List(str) + self._max_count = 14 if part2 else 4 + + @property + def chars(self) -> List[str]: + return self._chars + + @property + def is_valid(self) -> bool: + return self._max_count == self._chars.count() == self._chars.distinct().count() + + def add_char(self, c: str): + if self._chars.count() == self._max_count: + self._chars.remove_at(0) + + self._chars.add(c) + + +def part1(): + marker = Marker() + for i, c in enumerate(aoc_input): + marker.add_char(c) + if not marker.is_valid: + continue + + Console.write_line(f'Found marker: {"".join(marker.chars)} at {i + 1}') + break + + +def part2(): + marker = Marker(True) + for i, c in enumerate(aoc_input): + marker.add_char(c) + if not marker.is_valid: + continue + + Console.write_line(f'Found marker: {"".join(marker.chars)} at {i + 1}') + break + + +if __name__ == '__main__': + Console.write_line(f'Advent of code day {day}') + part1() + part2() + Console.write_line()