-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.a.py
34 lines (29 loc) · 784 Bytes
/
05.a.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
with open("2024/05.input.txt", encoding="utf-8") as file:
data = file.read()
lines = data.splitlines()
rules_: list[str] = []
updates_: list[str] = []
second = False
for line in lines:
if second:
if line == "":
continue
updates_.append(line)
else:
if line == "":
second = True
continue
rules_.append(line)
rules = [tuple(int(x) for x in rule.split("|")) for rule in rules_]
updates = [[int(x) for x in update.split(",")] for update in updates_]
total = 0
for update in updates:
for a, b in rules:
try:
if update.index(a) > update.index(b):
break
except ValueError:
continue
else:
total += update[len(update) // 2]
print(total)