-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.py
40 lines (32 loc) · 1010 Bytes
/
13.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
from utils import puzzle_input
from functools import cmp_to_key
inp = puzzle_input("13")
def solve1(inp=inp):
pairs = inp.split("\n\n")
total = 0
for i, pair in enumerate(pairs, start=1):
fst, snd = tuple(map(eval, pair.split("\n")))
if compare(fst, snd) < 0:
total += i
return total
def compare(l, r):
match l, r:
case int(), int():
return l - r
case int(), _:
return compare([l], r)
case _, int():
return compare(l, [r])
case list(), list():
for l_, r_ in zip(l, r):
if diff := compare(l_, r_):
return diff
return len(l) - len(r)
def solve2(inp=inp):
pairs = list(eval(line) for line in inp.split("\n") if line.strip())
dividers = ([[2]], [[6]])
pairs += dividers
pairs.sort(key=cmp_to_key(compare))
return (pairs.index(dividers[0]) + 1) * (pairs.index(dividers[1]) + 1)
print(solve1())
print(solve2())