+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanDay 18a849e3b

master
3.7 KiB149 linesraw
1import math
2import copy
3from functools import reduce as fold
4
5
6def add_first(l, dir, n):
7    if type(l[dir]) is list:
8        add_first(l[dir], dir, n)
9    else:
10        l[dir] += n
11
12
13def try_explode(l, d=0):
14    # print("exploding:", l, d)
15    a, b = l
16    if d == 3:
17        if type(a) is list:
18            temp = tuple(a)
19            l[0] = 0
20            if type(b) is int:
21                l[1] += temp[1]
22            else:
23                add_first(b, 0, temp[1])
24            return ("l", temp[0])
25        elif type(b) is list:
26            temp = tuple(b)
27            l[1] = 0
28            if type(a) is int:
29                l[0] += temp[0]
30            else:
31                add_first(a, 1, temp[0])
32            return ("r", temp[1])
33        return False
34    if type(a) is list:
35        result = try_explode(a, d + 1)
36        if result:
37            if type(result) is tuple:
38                # print("result", result, l)
39                dir, n = result
40                if dir == "l":
41                    return result
42                if type(b) is int:
43                    l[1] += n
44                else:
45                    add_first(b, 0, n)
46            return True
47    if type(b) is list:
48        result = try_explode(b, d + 1)
49        if result:
50            if type(result) is tuple:
51                dir, n = result
52                if dir == "r":
53                    return result
54                if type(a) is int:
55                    l[0] += n
56                else:
57                    add_first(a, 1, n)
58            return True
59    return False
60
61
62split = lambda n: [n // 2, math.ceil(n / 2)]
63
64
65def try_split(l):
66    a, b = l
67    if type(a) is list:
68        if try_split(a):
69            return True
70    elif a > 9:
71        l[0] = split(a)
72        return True
73    if type(b) is list:
74        if try_split(b):
75            return True
76    elif b > 9:
77        l[1] = split(b)
78        return True
79    return False
80
81
82def reduce(l):
83    while True:
84        if try_explode(l):
85            # print(l, "explode")
86            continue
87        if try_split(l):
88            # print(l, "split")
89            continue
90        # print("done reducing")
91        break
92
93
94def add(l, r):
95    temp = [copy.deepcopy(l), copy.deepcopy(r)]
96    # print(temp, "after add")
97    reduce(temp)
98    return temp
99
100
101# explode_tests = [
102#     ([[[[[9, 8], 1], 2], 3], 4], [[[[0, 9], 2], 3], 4]),
103#     ([7, [6, [5, [4, [3, 2]]]]], [7, [6, [5, [7, 0]]]]),
104#     ([[6, [5, [4, [3, 2]]]], 1], [[6, [5, [7, 0]]], 3]),
105#     (
106#         [[3, [2, [1, [7, 3]]]], [6, [5, [4, [3, 2]]]]],
107#         [[3, [2, [8, 0]]], [9, [5, [4, [3, 2]]]]],
108#     ),
109#     ([[3, [2, [8, 0]]], [9, [5, [4, [3, 2]]]]], [[3, [2, [8, 0]]], [9, [5, [7, 0]]]]),
110# ]
111# for a, b in explode_tests:
112#     try_explode(a)
113#     if str(a) != str(b):
114#         print(a, "should be", b)
115
116
117# print(add([[[[4, 3], 4], 4], [7, [[8, 4], 9]]], [1, 1]))
118
119
120def magnitude(l):
121    a, b = l
122    return 3 * (a if type(a) is int else magnitude(a)) + 2 * (
123        b if type(b) is int else magnitude(b)
124    )
125
126
127# test_magnitude = [
128#     ([[1, 2], [[3, 4], 5]], 143),
129#     ([[[[0, 7], 4], [[7, 8], [6, 0]]], [8, 1]], 1384),
130#     ([[[[1, 1], [2, 2]], [3, 3]], [4, 4]], 445),
131#     ([[[[3, 0], [5, 3]], [4, 4]], [5, 5]], 791),
132#     ([[[[5, 0], [7, 4]], [5, 5]], [6, 6]], 1137),
133#     ([[[[8, 7], [7, 7]], [[8, 6], [7, 7]]], [[[0, 7], [6, 6]], [8, 7]]], 3488),
134# ]
135#
136# for l, n in test_magnitude:
137#     if magnitude(l) != n:
138#         print(l, "should be", n, f"(was {magnitude(l)})")
139
140nums = list(map(eval, open("input").readlines()))
141print(magnitude(fold(add, nums)))
142
143most = 0
144for a in range(len(nums)):
145    for b in range(len(nums)):
146        if a == b:
147            continue
148        most = max(magnitude(add(nums[a], nums[b])), most)
149print(most)