-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.py
More file actions
66 lines (46 loc) · 1.53 KB
/
day13.py
File metadata and controls
66 lines (46 loc) · 1.53 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
import builtins
def read_input():
with open("inputs/day13.txt", "r") as fin:
groups = fin.read().split("\n\n")
pairs = list(map(lambda x: x.strip().split("\n"), groups))
return [(eval(lhs), eval(rhs)) for lhs, rhs in pairs]
def compare(lhs, rhs):
if len(lhs) == 0 and len(rhs) == 0:
return 0
elif len(lhs) == 0:
return -1
elif len(rhs) == 0:
return 1
lhead, *ltail = lhs
rhead, *rtail = rhs
match [type(lhead), type(rhead)]:
case [builtins.int, builtins.int]:
return -1 if lhead < rhead else 1 if lhead > rhead else compare(ltail, rtail)
case [builtins.list, builtins.list]:
return x if (x:=compare(lhead, rhead)) != 0 else compare(ltail, rtail)
case [builtins.int, builtins.list]:
return x if (x:=compare([lhead], rhead)) != 0 else compare(ltail, rtail)
case [builtins.list, builtins.int]:
return x if (x:=compare(lhead, [rhead])) != 0 else compare(ltail, rtail)
def problem_1():
acc = 0
inp = read_input()
for i, pair in enumerate(inp, 1):
if compare(*pair) == -1:
acc += i
return acc
def problem_2():
inp = read_input()
flatten_inp = []
for pair in inp:
flatten_inp.extend(pair)
div1, ind1 = [[2]], 1
div2, ind2 = [[6]], 2
for elem in flatten_inp:
if compare(elem, div1) == -1:
ind1 += 1
if compare(elem, div2) == -1:
ind2 += 1
return ind1*ind2
print(problem_1())
print(problem_2())