Skip to content

Commit 047ac79

Browse files
committed
Add day 13
1 parent 983ca32 commit 047ac79

File tree

7 files changed

+494
-1
lines changed

7 files changed

+494
-1
lines changed

2022/13/13.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import sys, functools
2+
3+
def cmp(a, b):
4+
if isinstance(a, int) and isinstance(b, int):
5+
return 0 if a == b else (-1 if a < b else 1)
6+
a = [a] if isinstance(a, int) else a
7+
b = [b] if isinstance(b, int) else b
8+
return ([cmp(*p) for p in zip(a, b) if cmp(*p) != 0] + [cmp(len(a), len(b))])[0]
9+
10+
lists = [eval(a) for a in sys.stdin.read().strip().replace("\n\n", "\n").split("\n")]
11+
print(sum(i for i, pair in enumerate(zip(lists[::2], lists[1::2]), 1) if cmp(*pair) <= 0))
12+
13+
new = sorted(lists + [[[2]], [[6]]], key=functools.cmp_to_key(cmp))
14+
print((new.index([[2]]) + 1) * (new.index([[6]]) + 1))

2022/13/example.ans

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
13
2+
140

2022/13/example.in

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[1,1,3,1,1]
2+
[1,1,5,1,1]
3+
4+
[[1],[2,3,4]]
5+
[[1],4]
6+
7+
[9]
8+
[[8,7,6]]
9+
10+
[[4,4],4,4]
11+
[[4,4],4,4,4]
12+
13+
[7,7,7,7]
14+
[7,7,7]
15+
16+
[]
17+
[3]
18+
19+
[[[]]]
20+
[[]]
21+
22+
[1,[2,[3,[4,[5,6,7]]]],8,9]
23+
[1,[2,[3,[4,[5,6,0]]]],8,9]

2022/13/input.ans

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
6076
2+
24805

0 commit comments

Comments
 (0)