summary refs log tree commit diff stats
path: root/day18.py
blob: 771130987ed97237a47ef340f003c555beab7bf6 (plain) (blame)
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
#!/usr/bin/env python

def unpack(line, n=1):
    '''flattens the number while also keeping track of level'''
    if line == []:
        return line
    if isinstance(line[0], list):
        return unpack(line[0], n+1) + unpack(line[1:], n)
    return [[line[0], n]] + unpack(line[1:], n)

with open('day18.txt') as data:
    nums = []
    for line in data:
        num = unpack(eval(line))
        nums.append(num)

def explode(line):
    for i in range(len(line)-1):
        num1, level1 = line[i]
        num2, level2 = line[i+1]
        if level1 == level2 == 5:
            line[i] = [0, 4]
            if i != 0:
                line[i-1][0] += num1
            if i+2 != len(line):
                line[i+2][0] += num2
            line.pop(i+1)
            return True
    return False

def split(line):
    for i in range(len(line)):
        num, level = line[i]
        if num >= 10:
            line[i:i+1] = [num//2, level+1], [num//2 + num%2, level+1]
            return True
    return False

def reduce_num(line):
    while True:
        if explode(line):
            continue
        if not split(line):
            break

    return line

def magnitude(num):
    while len(num) > 1:
        for i, ((num1, depth1), (num2, depth2)) in enumerate(zip(num, num[1:])):
            if depth1 != depth2: continue
            val = num1 * 3 + num2 * 2
            num = num[:i] + [[val, depth1-1]] + num[i+2:]
            break
    return num[0][0]

# part 1
curr = nums[0]
for line in nums[1:]:
    summation = list(map(lambda x: [x[0], x[1] + 1], curr + line))
    curr = reduce_num(summation)
print(magnitude(curr))

# part 2
from itertools import permutations
permute_nums = permutations(nums, 2)
maximum = 0
for perm in permute_nums:
    summation = reduce_num(list(map(lambda x: [x[0], x[1]+1], perm[0] + perm[1])))
    maximum = max(maximum, magnitude(summation))
print(maximum)