Skip to content

Commit 5656445

Browse files
committed
2022-13
1 parent 6c23c27 commit 5656445

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

2022/13.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
from math import prod
3+
from functools import cmp_to_key
4+
5+
6+
def cmp(left, right):
7+
match left, right:
8+
case int(), list():
9+
return cmp([left], right)
10+
case list(), int():
11+
return cmp(left, [right])
12+
case int(), int():
13+
return left - right
14+
case list(), list():
15+
for i, j in zip(left, right):
16+
if (r := cmp(i, j)) != 0:
17+
return r
18+
return cmp(len(left), len(right))
19+
20+
21+
# fmt: off
22+
packets = [
23+
eval(line)
24+
for line in open(0).read().splitlines()
25+
if len(line)
26+
]
27+
28+
print(sum(
29+
i
30+
for i, (left, right) in enumerate(zip(packets[::2], packets[1::2]), start=1)
31+
if cmp(left, right) <= 0
32+
))
33+
34+
two_six = [[[2]], [[6]]]
35+
print(prod(
36+
i
37+
for i, packet in enumerate(
38+
sorted(packets + two_six, key=cmp_to_key(cmp)),
39+
start=1
40+
)
41+
if packet in two_six
42+
))

0 commit comments

Comments
 (0)