-
Notifications
You must be signed in to change notification settings - Fork 1
/
13.py
77 lines (58 loc) · 1.69 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# --- Day 13: Distress Signal ---
import json
with open("input/13.txt") as f:
data = f.readlines()
pairs = []
for i in range(0, len(data), 3):
pair_1 = json.loads(data[i].strip())
pair_2 = json.loads(data[i+1].strip())
pairs.append((pair_1, pair_2))
results = []
def f(left, right):
if isinstance(left, int) and isinstance(right, int):
if left == right:
return None
return left < right
elif isinstance(left, list) and isinstance(right, int):
return f(left, [right])
elif isinstance(left, int) and isinstance(right, list):
return f([left], right)
elif isinstance(left, list) and isinstance(right, list):
if len(left) == 0 and len(right) == 0:
return None
if len(left) == 0:
return True
if len(right) == 0:
return False
res = f(left[0], right[0])
if res is None:
return f(left[1:], right[1:])
else:
return res
raise Exception("Invalid input")
for i, (p1, p2) in enumerate(pairs):
if f(p1, p2):
results.append(i+1)
print(sum(results))
# 6235
class Pair():
def __init__(self, value):
self.value = value
def __lt__(self, other):
return f(self.value, other.value)
def __eq__(self, other):
return self.value == other.value
new_pairs = []
for (left, right) in pairs:
new_pairs.append(Pair(left))
new_pairs.append(Pair(right))
divider_1 = Pair([[2]])
divider_2 = Pair([[6]])
new_pairs.append(divider_1)
new_pairs.append(divider_2)
res = []
for i,x in enumerate(sorted(new_pairs)):
if x == divider_1 or x == divider_2:
res.append(i + 1)
print(res[0] * res[1])
# 22866