-
Notifications
You must be signed in to change notification settings - Fork 4
/
day13.py
99 lines (87 loc) · 1.96 KB
/
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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Day 13: Distress Signal
"""
import ast
from collections.abc import Iterable
from itertools import zip_longest
SAMPLE_INPUT = [
"[1,1,3,1,1]",
"[1,1,5,1,1]",
"",
"[[1],[2,3,4]]",
"[[1],4]",
"",
"[9]",
"[[8,7,6]]",
"",
"[[4,4],4,4]",
"[[4,4],4,4,4]",
"",
"[7,7,7,7]",
"[7,7,7]",
"",
"[]",
"[3]",
"",
"[[[]]]",
"[[]]",
"",
"[1,[2,[3,[4,[5,6,7]]]],8,9]",
"[1,[2,[3,[4,[5,6,0]]]],8,9]",
]
def _parse(line):
if any(char not in "\n,0123456789[]" for char in line):
raise ValueError()
return ast.literal_eval(line)
def _compare(lhs, rhs):
if not isinstance(lhs, Iterable) and not isinstance(rhs, Iterable):
return 0 if lhs == rhs else -1 if lhs < rhs else 1
try:
lhs = iter(lhs)
except TypeError:
lhs = iter((lhs,))
try:
rhs = iter(rhs)
except TypeError:
rhs = iter((rhs,))
while True:
try:
left = next(lhs)
except StopIteration:
try:
next(rhs)
return -1
except StopIteration:
return 0
try:
right = next(rhs)
except StopIteration:
return 1
comparison = _compare(left, right)
if comparison:
return comparison
def part1(lines):
"""
>>> part1(SAMPLE_INPUT)
13
"""
total = 0
for i, pair in enumerate(zip_longest(*(iter(lines),) * 3)):
if _compare(_parse(pair[0]), _parse(pair[1])) <= 0:
total += i + 1
return total
def part2(lines):
"""
>>> part2(SAMPLE_INPUT)
140
"""
divider1, divider2 = [[2]], [[6]]
count1, count2 = 1, 1
for line in filter(str.strip, lines):
packet = _parse(line)
if _compare(packet, divider1) < 0:
count1 += 1
elif _compare(packet, divider2) < 0:
count2 += 1
return count1 * (count1 + count2)
parts = (part1, part2)