+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
a849e3b
master
1import math 2import copy 3from functools import reduce as fold 4 5 6def add_first (l ,dir ,n ): 7if type (l [dir ])is list : 8add_first (l [dir ],dir ,n ) 9else : 10l [dir ]+= n 11 12 13def try_explode (l ,d = 0 ): 14# print("exploding:", l, d) 15a ,b = l 16if d == 3 : 17if type (a )is list : 18temp = tuple (a ) 19l [0 ]= 0 20if type (b )is int : 21l [1 ]+= temp [1 ] 22else : 23add_first (b ,0 ,temp [1 ]) 24return ("l" ,temp [0 ]) 25elif type (b )is list : 26temp = tuple (b ) 27l [1 ]= 0 28if type (a )is int : 29l [0 ]+= temp [0 ] 30else : 31add_first (a ,1 ,temp [0 ]) 32return ("r" ,temp [1 ]) 33return False 34if type (a )is list : 35result = try_explode (a ,d + 1 ) 36if result : 37if type (result )is tuple : 38# print("result", result, l) 39dir ,n = result 40if dir == "l" : 41return result 42if type (b )is int : 43l [1 ]+= n 44else : 45add_first (b ,0 ,n ) 46return True 47if type (b )is list : 48result = try_explode (b ,d + 1 ) 49if result : 50if type (result )is tuple : 51dir ,n = result 52if dir == "r" : 53return result 54if type (a )is int : 55l [0 ]+= n 56else : 57add_first (a ,1 ,n ) 58return True 59return False 60 61 62split = lambda n : [n // 2 ,math .ceil (n / 2 )] 63 64 65def try_split (l ): 66a ,b = l 67if type (a )is list : 68if try_split (a ): 69return True 70elif a > 9 : 71l [0 ]= split (a ) 72return True 73if type (b )is list : 74if try_split (b ): 75return True 76elif b > 9 : 77l [1 ]= split (b ) 78return True 79return False 80 81 82def reduce (l ): 83while True : 84if try_explode (l ): 85# print(l, "explode") 86continue 87if try_split (l ): 88# print(l, "split") 89continue 90# print("done reducing") 91break 92 93 94def add (l ,r ): 95temp = [copy .deepcopy (l ),copy .deepcopy (r )] 96# print(temp, "after add") 97reduce (temp ) 98return 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 ): 121a ,b = l 122return 3 * (a if type (a )is int else magnitude (a ))+ 2 * ( 123b 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 ())) 141magnitude (fold (add ,nums ))) 142 143most = 0 144for a in range (len (nums )): 145for b in range (len (nums )): 146if a == b : 147continue 148most = max (magnitude (add (nums [a ],nums [b ])),most ) 149most )