Compare commits

...

2 commits

Author SHA1 Message Date
Evie Litherland-Smith fb28af7e0a Start on d5p1 2024-12-24 10:15:16 +00:00
Evie Litherland-Smith 86680b4174 Finished day 4 2024-12-24 09:20:59 +00:00
2 changed files with 79 additions and 2 deletions

View file

@ -28,7 +28,7 @@ class FindXMAS:
@property @property
def _part2(self) -> int: def _part2(self) -> int:
return len(self.locate_x_mas()) return len(self.locate_x_mas()) // 2
@staticmethod @staticmethod
def _to_grid(input: str) -> List[List[str]]: def _to_grid(input: str) -> List[List[str]]:
@ -98,13 +98,14 @@ class FindXMAS:
if loc[1] in oth: if loc[1] in oth:
locations.append(loc) locations.append(loc)
return mas return locations
if __name__ == "__main__": if __name__ == "__main__":
example = FindXMAS(EXAMPLE) example = FindXMAS(EXAMPLE)
print(example) print(example)
assert example._part1 == 18 assert example._part1 == 18
assert example._part2 == 9
with open("input.txt", "r") as f: with open("input.txt", "r") as f:
puzzle = FindXMAS(f.read()) puzzle = FindXMAS(f.read())
print(puzzle) print(puzzle)

76
day5/run.py Normal file
View file

@ -0,0 +1,76 @@
from typing import Callable, List
EXAMPLE = """
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47
"""
class CheckManual:
def __init__(self, input: str) -> None:
self._input = input[1:]
sep = self._input.splitlines().index("")
self.rules = self.parse_rules(self._input.splitlines()[:sep])
self.pages = self._input.splitlines()[sep:]
def __str__(self) -> str:
return f"Part1: {self._part1}\nPart2: {self._part2}"
@property
def _part1(self) -> int:
return None
@property
def _part2(self) -> int:
return None
def parse_rules(
self, rules: List[str], sep: str = "|"
) -> List[Callable[[str], bool]]:
def make_rule(rule: str, sep: str = "|") -> Callable[[str], bool]:
left, right = rule.split(sep)
def check(pages: str) -> bool:
_pages = pages.split(",")
try:
_pages.index(left) < _pages.index(right)
except ValueError:
return True
return check
return [make_rule(rule) for rule in rules]
def check_page_order(self) -> List[bool]:
return [all([check(line) for check in self.rules]) for line in self.pages]
if __name__ == "__main__":
example = CheckManual(EXAMPLE)
print(example)