-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday13.py
executable file
·43 lines (28 loc) · 955 Bytes
/
day13.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
#!/usr/bin/env python3
from json import loads
from functools import cmp_to_key
import sys
def compare(a, b):
a_is_int = type(a) is int
b_is_int = type(b) is int
if a_is_int and b_is_int:
return a - b
if a_is_int ^ b_is_int:
return compare([a], b) if a_is_int else compare(a, [b])
for x, y in zip(a, b):
res = compare(x, y)
if res != 0:
return res
return len(a) - len(b)
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
lines = fin.read().replace('\n\n', '\n').splitlines()
packets = list(map(loads, lines))
pairs = (packets[i:i + 2] for i in range(0, len(packets), 2))
answer = sum(i for i, p in enumerate(pairs, 1) if compare(*p) < 0)
print('Part 1:', answer)
packets.extend(([[2]], [[6]]))
packets.sort(key=cmp_to_key(compare))
answer = packets.index([[2]]) + 1
answer *= packets.index([[6]], answer) + 1
print('Part 2:', answer)