-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.py
More file actions
85 lines (55 loc) · 1.8 KB
/
day13.py
File metadata and controls
85 lines (55 loc) · 1.8 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
"""
Parse different types of data.
Timing:
python3 = 34 ms
"""
from functools import cmp_to_key
import json
from aoc import list_flatten, map_list, read_input
# ------------------------------------------------------------------------------
def compare(a, b):
"""
= a - b
a < b => -x
a = b => 0
a > b => +x
"""
if type(a) == list and type(b) == list:
for left, right in zip(a, b):
if (res := compare(left, right)) != 0:
return res
return len(a) - len(b)
if type(a) == list and type(b) == int:
return compare(a, [b])
if type(a) == int and type(b) == list:
return compare([a], b)
# default: a and b are int
return a - b
def parse_packet(txt: str):
# split line and parse with json
return map_list(json.loads, txt.splitlines())
def solve_part1(packets):
# Calculate the sum of index when left < right.
return sum(idx if compare(*packet) < 0 else 0 for idx, packet in enumerate(packets, 1))
def solve_part2(packets):
packets = packets + [[2]] + [[6]]
packets = list_flatten(packets)
# use 'cmp_to_key' to use comparison function
packets = sorted(packets, key=cmp_to_key(compare))
index_two = packets.index(2) + 1
index_six = packets.index(6) + 1
return index_two * index_six
def solve(day=13, test=False):
txt = read_input(day, test).split("\n\n")
packets = map_list(parse_packet, txt)
part1 = solve_part1(packets)
part2 = solve_part2(packets)
return part1, part2
# ------------------------------------------------------------------------------
# res = solve(test=True)
# assert res == (13, 140)
res = solve()
assert res == (6086, 27930)
print(*res)
# ------------------------------------------------------------------------------