-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_13.py
54 lines (43 loc) · 1.14 KB
/
day_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from helper import get_input
import json
from functools import cmp_to_key
inp = get_input(day=13)
def is_inorder(a, b):
match a, b:
case int(), list():
return is_inorder([a], b)
case list(), int():
return is_inorder(a, [b])
case int(), int():
return a - b
case list(), list():
for i, j in zip(a, b):
if (r := is_inorder(i, j)) != 0:
return r
return is_inorder(len(a), len(b))
PACKETS = [[[2]], [[6]]]
def part_1():
res = 0
pair = 1
for i in range(0, len(inp), 3):
a = json.loads(inp[i])
b = json.loads(inp[i + 1])
PACKETS.append(a)
PACKETS.append(b)
if is_inorder(a, b) <= 0:
res += pair
pair += 1
return res
def part_2():
two = None
six = None
global PACKETS
PACKETS = sorted(PACKETS, key=cmp_to_key(is_inorder))
for i, pkt in enumerate(PACKETS, start=1):
if pkt == [[2]]:
two = i
elif pkt == [[6]]:
six = i
return two * six
print(f"D13P1: {part_1()}")
print(f"D13P2: {part_2()}")