Finish day5p1
This commit is contained in:
parent
fb28af7e0a
commit
0e026e3ba4
1370
day5/input.txt
Normal file
1370
day5/input.txt
Normal file
File diff suppressed because it is too large
Load diff
20
day5/run.py
20
day5/run.py
|
@ -37,14 +37,21 @@ class CheckManual:
|
||||||
self._input = input[1:]
|
self._input = input[1:]
|
||||||
sep = self._input.splitlines().index("")
|
sep = self._input.splitlines().index("")
|
||||||
self.rules = self.parse_rules(self._input.splitlines()[:sep])
|
self.rules = self.parse_rules(self._input.splitlines()[:sep])
|
||||||
self.pages = self._input.splitlines()[sep:]
|
self.pages = [val.split(",") for val in self._input.splitlines()[sep + 1 :]]
|
||||||
|
self.validity = self.check_page_order()
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"Part1: {self._part1}\nPart2: {self._part2}"
|
return f"Part1: {self._part1}\nPart2: {self._part2}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _part1(self) -> int:
|
def _part1(self) -> int:
|
||||||
return None
|
return sum(
|
||||||
|
[
|
||||||
|
int(val[len(val) // 2])
|
||||||
|
for val, check in zip(self.pages, self.validity)
|
||||||
|
if check
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _part2(self) -> int:
|
def _part2(self) -> int:
|
||||||
|
@ -56,10 +63,9 @@ class CheckManual:
|
||||||
def make_rule(rule: str, sep: str = "|") -> Callable[[str], bool]:
|
def make_rule(rule: str, sep: str = "|") -> Callable[[str], bool]:
|
||||||
left, right = rule.split(sep)
|
left, right = rule.split(sep)
|
||||||
|
|
||||||
def check(pages: str) -> bool:
|
def check(pages: List[str]) -> bool:
|
||||||
_pages = pages.split(",")
|
|
||||||
try:
|
try:
|
||||||
_pages.index(left) < _pages.index(right)
|
return pages.index(left) < pages.index(right)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -74,3 +80,7 @@ class CheckManual:
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
example = CheckManual(EXAMPLE)
|
example = CheckManual(EXAMPLE)
|
||||||
print(example)
|
print(example)
|
||||||
|
assert example._part1 == 143
|
||||||
|
with open("input.txt", "r") as f:
|
||||||
|
main = CheckManual(f.read())
|
||||||
|
print(main)
|
||||||
|
|
Loading…
Reference in a new issue