day 6
This commit is contained in:
parent
faa8d01843
commit
7de54241f1
@ -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
64
src/day6.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user