Skip to content

Commit fbcb356

Browse files
committed
Add day 5
1 parent e8cc040 commit fbcb356

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

2024/05/05.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
rules, pages = open(0).read().split("\n\n")
22
rules = {tuple(r.split("|")) for r in rules.splitlines()}
33

4-
s1 = s2 = 0
4+
s = [0, 0]
55
for row in pages.splitlines():
6-
n = row.split(",")
7-
n2 = n[:]
8-
if all(n.index(b) < n.index(a) for b, a in rules if b in n and a in n):
9-
s1 += int(n[len(n)//2])
10-
else:
11-
new = []
12-
i = -1
13-
while n:
14-
i = (i + 1) % len(n)
15-
if all(b not in n or b in new for b, a in rules if n[i] == a):
16-
new.append(n.pop(i))
17-
s2 += int(new[len(new)//2])
18-
19-
print(s1, s2, sep="\n")
6+
old, new = row.split(","), []
7+
for o in old * 100:
8+
if o in new: continue
9+
if all(b in new for b, a in rules if o == a and b in old):
10+
new.append(o)
11+
s[new != old] += int(new[len(new)//2])
12+
13+
print(*s, sep="\n")

0 commit comments

Comments
 (0)