This commit is contained in:
Sven Heidemann 2022-12-06 07:49:16 +01:00
parent faa8d01843
commit 7de54241f1
2 changed files with 67 additions and 1 deletions

View File

@ -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()

64
src/day6.py Normal file
View File

@ -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()