-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
80 lines (65 loc) · 2.42 KB
/
script.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
import sys
import json
import functools
inputs = (line.rstrip('\n') for line in open(sys.argv[1]))
pairs = []
while True:
pair = (json.loads(next(inputs)), json.loads(next(inputs)))
pairs.append(pair)
try:
next(inputs)
except StopIteration:
break
def indent_s(level):
return ' '*level*2
def compare(left, right, indent=0):
# print(f'{indent_s(indent)}- Compare {str(left).replace(" ", "")} vs {str(right).replace(" ","")}')
# both numbers
if not isinstance(left, list) and not isinstance(right, list):
# print(f'{indent_s(indent+1)}- Compare {left} vs {right}')
if left > right:
# print(f'{indent_s(indent+2)}- Right side is smaller, so inputs are not in the right order')
return 1
elif left < right:
# print(f'{indent_s(indent+2)}- Left side is smaller, so inputs are in the right order')
return -1
else:
return 0
# left number, right list
elif not isinstance(left, list) and isinstance(right, list):
# print(f'{indent_s(indent+1)}- Compare {left} vs {str(right).replace(" ","")}')
# print(f'{indent_s(indent+2)}- Mixed types; convert left to [{left}] and retry comparison')
return compare([left], right, indent+2)
# left list, right number
elif isinstance(left, list) and not isinstance(right, list):
# print(f'{indent_s(indent+1)}- Compare {str(left).replace(" ","")} vs {right}')
# print(f'{indent_s(indent+2)}- Mixed types; convert right to [{right}] and retry comparison')
return compare(left, [right], indent+2)
# both lists, neither empty
elif left and right:
result = compare(left[0], right[0], indent+1)
if result:
return result
else:
return compare(left[1:], right[1:])
# both lists, either/both empty
elif left:
return 1
elif right:
return -1
else:
return 0
packets = [[[2]], [[6]]]
valid_pairs = []
for idx, pair in enumerate(pairs):
one, two = pair
# print(f'== Pair {idx+1} ==')
if compare(one, two) in [-1]:
valid_pairs.append(idx+1)
# print()
packets += [one, two]
print(f'{sum(valid_pairs)} | Valid: {valid_pairs}')
sorted_packets = sorted(packets, key=functools.cmp_to_key(compare))
# for packet in sorted_packets:
# print(packet)
print((sorted_packets.index([[2]]) + 1) * (sorted_packets.index([[6]]) + 1))