-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_05.py
45 lines (37 loc) · 1.53 KB
/
day_05.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from models.aoc_solution import AOCSolution
class Day05(AOCSolution):
EXPECTED = {
"part_one": {"sample": 143, "data": 4662},
"part_two": {"sample": 123, "data": 5900},
}
def __post_init__(self) -> None:
rules, updates = map(str.splitlines, self.data.split("\n\n"))
self.rules = {tuple(map(int, rule.split("|"))) for rule in rules}
self.updates = [list(map(int, update.split(","))) for update in updates]
def invalid_positions(self, update: list[int]) -> tuple[int, int] | None:
"""Find first invalid position (if any) baded on the ordering rules"""
for i, page in enumerate(update):
for j, after in enumerate(update[i + 1 :], start=i + 1):
if (after, page) in self.rules:
return i, j
def part_one(self) -> int:
"""Sum the middle number of valid updates"""
return sum(
update[len(update) // 2]
for update in self.updates
if not self.invalid_positions(update)
)
def part_two(self) -> int:
"""Sum the middle number of fixed invalid updates"""
total = 0
for update in self.updates:
if not (coords := self.invalid_positions(update)):
continue
while coords:
i, j = coords
update[i], update[j] = update[j], update[i]
coords = self.invalid_positions(update)
total += update[len(update) // 2]
return total
if __name__ == "__main__":
Day05().run()