-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistress_signal.py
53 lines (36 loc) · 1.37 KB
/
distress_signal.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
from utils import read_input, run
from functools import cmp_to_key
import json
FNAME = "13/input.txt"
##########
# PART 1 #
##########
def compare(left, right):
if isinstance(left, int) and isinstance(right, int):
if left == right:
return 0
return left - right
if isinstance(left, list) and isinstance(right, list):
for left_v, right_v in zip(left, right):
if (result := compare(left_v, right_v)) != 0:
return result
if len(left) == len(right):
return 0
return len(left) - len(right)
if isinstance(left, int):
return compare([left], right)
if isinstance(right, int):
return compare(left, [right])
def part_one(input_file):
data = read_input(input_file, separator='\n\n', parse_chunk=lambda c: tuple(map(json.loads, c.split('\n'))))
return sum(i+1 for i, diff in enumerate(compare(l, r) for l, r in data) if diff < 0)
##########
# PART 2 #
##########
def part_two(input_file):
data = list(filter(lambda x: x is not None, read_input(input_file, separator='\n', parse_chunk=lambda c: json.loads(c) if c else None)))
data += [[[2]], [[6]]]
sorted_data = sorted(data, key=cmp_to_key(compare))
return (sorted_data.index([[2]]) + 1) * (sorted_data.index([[6]]) + 1)
if __name__ == '__main__':
run(part_one, part_two, FNAME)