aboutsummaryrefslogtreecommitdiff
path: root/2021/day18/18.py
blob: 88ebf5e0cc5232f2d6ab9ee23c1105d468ab11d2 (plain)
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
130
131
132
from math import ceil

class SnailfishItem:
    def __init__(self, parent, contents):
        self.parent = parent
        self.atomic = not isinstance(contents, list)
        if not self.atomic:
            if isinstance(contents[0], SnailfishItem):
                self.left = contents[0]
                self.left.parent = self
            else:
                self.left = SnailfishItem(self, contents[0])
            if isinstance(contents[1], SnailfishItem):
                self.right = contents[1]
                self.right.parent = self
            else:
                self.right = SnailfishItem(self, contents[1])
        else:
            self.contents = contents

    def __str__(self):
        if self.atomic:
            return str(self.contents)
        else:
            return "[%s, %s]" % (str(self.left), str(self.right))

    def add(self, other):
        if self.atomic and other.atomic:
            self.contents += other.contents
        else:
            if self.atomic:
                self.left = SnailfishItem(self, self.contents)
            else:
                self.left = SnailfishItem(self, [self.left, self.right])
            self.atomic = False
            self.right = other
            other.parent = self
            self.contents = None
            while self.reduce():
                pass

    def check_explodes(self, n=0):
        if self.atomic:
            return False
        elif n < 4:
            return self.left.check_explodes(n+1) or self.right.check_explodes(n+1)

        self.explode_left()
        self.explode_right()

        self.atomic = True
        self.contents = 0
        self.left = None
        self.right = None

        return True

    def check_splits(self):
        if not self.atomic:
            return self.left.check_splits() or self.right.check_splits()
        elif self.contents < 10:
            return False

        self.atomic = False
        self.left = SnailfishItem(self, self.contents // 2)
        self.right = SnailfishItem(self, ceil(self.contents / 2))
        self.contents = None
        return True

    def reduce(self, n=0):
        if not self.atomic:
            return self.check_explodes(n) or self.check_splits()
        else:
            return self.check_splits()

    def explode_left(self):
        last_node = self
        node = self.parent
        while node != None and node.left == last_node:
            last_node = node
            node = last_node.parent

        if node == None:
            return # leftmost element of tree

        node = node.left
        while not node.atomic:
            node = node.right

        node.add(self.left)

    def explode_right(self):
        last_node = self
        node = self.parent
        while node != None and node.right == last_node:
            last_node = node
            node = last_node.parent

        if node == None:
            return # rightmost element of tree

        node = node.right
        while not node.atomic:
            node = node.left

        node.add(self.right)

    def magnitude(self):
        if self.atomic:
            return self.contents
        else:
            return (3 * self.left.magnitude()) + (2 * self.right.magnitude())

lines = open("./input").read().strip().split("\n")
val = SnailfishItem(None, eval(lines[0])) # cope, seethe, mald, etc.
for line in lines[1:]:
    val.add(SnailfishItem(None, eval(line)))

print("Part 1: %d" % val.magnitude())

max_mag = 0
for x_str in lines:
    for y_str in lines:
        if x_str == y_str:
            continue
        x = SnailfishItem(None, eval(x_str))
        x.add(SnailfishItem(None, eval(y_str)))

        if x.magnitude() > max_mag:
            max_mag = x.magnitude()

print("Part 2: %d" % max_mag)