-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.py
executable file
·129 lines (102 loc) · 3.1 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#! /usr/bin/env python3
import copy
import sys
import pprint
# -1 = in order | 0 = continue | 1 = fail
def list_cmp(left_list: list, right_list: list) -> int:
while True:
if len(left_list) == 0 and len(right_list) == 0:
return 0
if len(left_list) == 0:
return -1
if len(right_list) == 0:
return 1
l = left_list.pop(0)
r = right_list.pop(0)
if isinstance(l, int) and isinstance(r, int):
if l < r:
return -1
if l > r:
return 1
if isinstance(l, int) and isinstance(r, list):
x = list_cmp([l], r)
if x == -1:
return -1
if x == 1:
return 1
if isinstance(l, list) and isinstance(r, int):
x = list_cmp(l, [r])
if x == -1:
return -1
if x == 1:
return 1
if isinstance(l, list) and isinstance(r, list):
x = list_cmp(l, r)
if x == -1:
return -1
if x == 1:
return 1
def pair_is_valid(pair) -> bool:
left_list, right_list = pair
print(f"\n {left_list = }\n{right_list = }")
while True:
if len(left_list) == 0:
return True
if len(right_list) == 0:
return False
l = left_list.pop(0)
r = right_list.pop(0)
if isinstance(l, int) and isinstance(r, int):
if l < r:
return True
if l > r:
return True
if isinstance(l, int) and isinstance(r, list):
x = list_cmp([l], r)
if x == -1:
return True
if x == 1:
return False
if isinstance(l, list) and isinstance(r, int):
x = list_cmp(l, [r])
if x == -1:
return True
if x == 1:
return False
if isinstance(l, list) and isinstance(r, list):
x = list_cmp(l, r)
if x == -1:
return True
if x == 1:
return False
if __name__ == "__main__":
packet_pairs = []
p2pairs = []
pairs = sys.argv[1].split("\n\n")
for p in pairs:
spl = p.split("\n")
packet_pairs.append((eval(spl[0]), eval(spl[1])))
p2pairs.append(eval(spl[0]))
p2pairs.append(eval(spl[1]))
count = 0
for idx, pair in enumerate(packet_pairs):
if pair_is_valid(pair):
count += idx + 1
# part 2
smaller_than_two = 1
smaller_than_six = 2
print(f"{p2pairs = }")
for pair in p2pairs:
if pair_is_valid((copy.deepcopy(pair), [[2]])):
print(f"{pair} < 2")
smaller_than_two += 1
print(f"{p2pairs = }")
for pair in p2pairs:
if pair_is_valid((copy.deepcopy(pair), [[6]])):
print(f"{pair} < 6")
smaller_than_six += 1
print(f"{count = }")
print(f"{len(p2pairs) =}")
print(
f"{smaller_than_two = } {smaller_than_six = } a = {smaller_than_two * smaller_than_six}"
)