-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_one.py
47 lines (32 loc) · 1.19 KB
/
part_one.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
46
47
from collections import defaultdict
from typing import override
from infrastructure.solutions.base import Solution
class Year2024Day5Part1Solution(Solution):
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, dict | list]:
rules = defaultdict(set)
updates = []
page_ordering, page_updates = text_input.split('\n\n')
for rule in page_ordering.split('\n'):
first, second = map(int, rule.split('|'))
rules[first].add(second)
for update in page_updates.split('\n'):
updates.append(list(map(int, update.split(','))))
return {'rules': rules, 'updates': updates}
@classmethod
@override
def solve(cls, rules: dict[int, set], updates: list[list[int]]) -> int:
valid_sum = 0
for update in updates:
is_valid = True
seen = set()
for page in update:
if rules[page] & seen:
is_valid = False
seen.add(page)
if is_valid:
valid_sum += update[len(update) // 2]
return valid_sum
if __name__ == '__main__':
print(Year2024Day5Part1Solution.main())