part1 (8200B)
1--- Day 18: Snailfish --- 2 3You descend into the ocean trench and encounter some snailfish. They say they saw the sleigh keys! 4They'll even tell you which direction the keys went if you help one of the smaller snailfish with 5his [1m[97mmath homework[0m. 6 7Snailfish numbers aren't like regular numbers. Instead, every snailfish number is a 8[1m[97mpair[0m - an ordered list of two elements. Each element of the pair can be either a regular number or 9another pair. 10 11Pairs are written as [x,y], where x and y are the elements within the pair. Here are some example 12snailfish numbers, one snailfish number per line: 13 14[1,2] 15[[1,2],3] 16[9,[8,7]] 17[[1,9],[8,5]] 18[[[[1,2],[3,4]],[[5,6],[7,8]]],9] 19[[[9,[3,8]],[[0,9],6]],[[[3,7],[4,9]],3]] 20[[[[1,3],[5,3]],[[1,3],[8,7]]],[[[4,9],[6,9]],[[8,2],[7,3]]]] 21 22This snailfish homework is about [1m[97maddition[0m. To add two snailfish numbers, form a pair from the left 23and right parameters of the addition operator. For example, [1,2] + [[3,4],5] becomes 24[[1,2],[[3,4],5]]. 25 26There's only one problem: [1m[97msnailfish numbers must always be reduced[0m, and the process of adding two 27snailfish numbers can result in snailfish numbers that need to be reduced. 28 29To [1m[97mreduce a snailfish number[0m, you must repeatedly do the first action in this list that applies to 30the snailfish number: 31 32 33 - If any pair is [1m[97mnested inside four pairs[0m, the leftmost such pair [1m[97mexplodes[0m. 34 35 - If any regular number is [1m[97m10 or greater[0m, the leftmost such regular number [1m[97msplits[0m. 36 37 38Once no action in the above list applies, the snailfish number is reduced. 39 40During reduction, at most one action applies, after which the process returns to the top of the list 41of actions. For example, if [1m[97msplit[0m produces a pair that meets the [1m[97mexplode[0m criteria, that pair 42[1m[97mexplodes[0m before other [1m[97msplits[0m occur. 43 44To [1m[97mexplode[0m a pair, the pair's left value is added to the first regular number to the left of the 45exploding pair (if any), and the pair's right value is added to the first regular number to the 46right of the exploding pair (if any). Exploding pairs will always consist of two regular numbers. 47Then, the entire exploding pair is replaced with the regular number 0. 48 49Here are some examples of a single explode action: 50 51 52 - [[[[[1m[97m[9,8][0m,1],2],3],4] becomes [[[[[1m[97m0[0m,[1m[97m9[0m],2],3],4] (the 9 has no regular number to its left, so it 53is not added to any regular number). 54 55 - [7,[6,[5,[4,[1m[97m[3,2][0m]]]] becomes [7,[6,[5,[[1m[97m7[0m,[1m[97m0[0m]]]] (the 2 has no regular number to its right, and so 56it is not added to any regular number). 57 58 - [[6,[5,[4,[1m[97m[3,2][0m]]],1] becomes [[6,[5,[[1m[97m7[0m,[1m[97m0[0m]]],[1m[97m3[0m]. 59 60 - [[3,[2,[1,[1m[97m[7,3][0m]]],[6,[5,[4,[3,2]]]]] becomes 61[[3,[2,[[1m[97m8[0m,[1m[97m0[0m]]],[[1m[97m9[0m,[5,[4,[3,2]]]]] (the pair [3,2] is unaffected because the pair [7,3] is further to 62the left; [3,2] would explode on the next action). 63 64 - [[3,[2,[8,0]]],[9,[5,[4,[1m[97m[3,2][0m]]]] becomes 65[[3,[2,[8,0]]],[9,[5,[[1m[97m7[0m,[1m[97m0[0m]]]]. 66 67 68To [1m[97msplit[0m a regular number, replace it with a pair; the left element of the pair should be the 69regular number divided by two and rounded [1m[97mdown[0m, while the right element of the pair should be the 70regular number divided by two and rounded [1m[97mup[0m. For example, 10 becomes [5,5], 11 becomes [5,6], 12 71becomes [6,6], and so on. 72 73Here is the process of finding the reduced result of [[[[4,3],4],4],[7,[[8,4],9]]] + [1,1]: 74 75after addition: [[[[[1m[97m[4,3][0m,4],4],[7,[[8,4],9]]],[1,1]] 76after explode: [[[[0,7],4],[7,[[1m[97m[8,4][0m,9]]],[1,1]] 77after explode: [[[[0,7],4],[[1m[97m15[0m,[0,13]]],[1,1]] 78after split: [[[[0,7],4],[[7,8],[0,[1m[97m13[0m]]],[1,1]] 79after split: [[[[0,7],4],[[7,8],[0,[1m[97m[6,7][0m]]],[1,1]] 80after explode: [[[[0,7],4],[[7,8],[6,0]]],[8,1]] 81 82Once no reduce actions apply, the snailfish number that remains is the actual result of the addition 83operation: [[[[0,7],4],[[7,8],[6,0]]],[8,1]]. 84 85The homework assignment involves adding up a [1m[97mlist of snailfish numbers[0m (your puzzle input). The 86snailfish numbers are each listed on a separate line. Add the first snailfish number and the second, 87then add that result and the third, then add that result and the fourth, and so on until all numbers 88in the list have been used once. 89 90For example, the final sum of this list is [[[[1,1],[2,2]],[3,3]],[4,4]]: 91 92[1,1] 93[2,2] 94[3,3] 95[4,4] 96 97The final sum of this list is [[[[3,0],[5,3]],[4,4]],[5,5]]: 98 99[1,1] 100[2,2] 101[3,3] 102[4,4] 103[5,5] 104 105The final sum of this list is [[[[5,0],[7,4]],[5,5]],[6,6]]: 106 107[1,1] 108[2,2] 109[3,3] 110[4,4] 111[5,5] 112[6,6] 113 114Here's a slightly larger example: 115 116[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]] 117[7,[[[3,7],[4,3]],[[6,3],[8,8]]]] 118[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]] 119[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]] 120[7,[5,[[3,8],[1,4]]]] 121[[2,[2,2]],[8,[8,1]]] 122[2,9] 123[1,[[[9,3],9],[[9,0],[0,7]]]] 124[[[5,[7,4]],7],1] 125[[[[4,2],2],6],[8,7]] 126 127The final sum [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] is found after adding up the 128above snailfish numbers: 129 130 [[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]] 131+ [7,[[[3,7],[4,3]],[[6,3],[8,8]]]] 132= [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]] 133 134 [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]] 135+ [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]] 136= [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]] 137 138 [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]] 139+ [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]] 140= [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]] 141 142 [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]] 143+ [7,[5,[[3,8],[1,4]]]] 144= [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]] 145 146 [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]] 147+ [[2,[2,2]],[8,[8,1]]] 148= [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]] 149 150 [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]] 151+ [2,9] 152= [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]] 153 154 [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]] 155+ [1,[[[9,3],9],[[9,0],[0,7]]]] 156= [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]] 157 158 [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]] 159+ [[[5,[7,4]],7],1] 160= [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]] 161 162 [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]] 163+ [[[[4,2],2],6],[8,7]] 164= [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] 165 166To check whether it's the right answer, the snailfish teacher only checks the [1m[97mmagnitude[0m of the final 167sum. The magnitude of a pair is 3 times the magnitude of its left element plus 2 times the magnitude 168of its right element. The magnitude of a regular number is just that number. 169 170For example, the magnitude of [9,1] is 3*9 + 2*1 = [1m[97m29[0m; the magnitude of [1,9] is 3*1 + 2*9 = 171[1m[97m21[0m. Magnitude calculations are recursive: the magnitude of [[9,1],[1,9]] is 3*29 + 2*21 = 172[1m[97m129[0m. 173 174Here are a few more magnitude examples: 175 176 177 - [[1,2],[[3,4],5]] becomes [1m[97m143[0m. 178 179 - [[[[0,7],4],[[7,8],[6,0]]],[8,1]] becomes [1m[97m1384[0m. 180 181 - [[[[1,1],[2,2]],[3,3]],[4,4]] becomes [1m[97m445[0m. 182 183 - [[[[3,0],[5,3]],[4,4]],[5,5]] becomes [1m[97m791[0m. 184 185 - [[[[5,0],[7,4]],[5,5]],[6,6]] becomes [1m[97m1137[0m. 186 187 - [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] becomes [1m[97m3488[0m. 188 189 190So, given this example homework assignment: 191 192[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]] 193[[[5,[2,8]],4],[5,[[9,9],0]]] 194[6,[[[6,2],[5,6]],[[7,6],[4,7]]]] 195[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]] 196[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]] 197[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]] 198[[[[5,4],[7,7]],8],[[8,3],8]] 199[[9,3],[[9,9],[6,[4,9]]]] 200[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] 201[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]] 202 203The final sum is: 204 205[[[[6,6],[7,6]],[[7,7],[7,0]]],[[[7,7],[7,7]],[[7,8],[9,9]]]] 206The magnitude of this final sum is [1m[97m4140[0m. 207 208Add up all of the snailfish numbers from the homework assignment in the order they appear. 209[1m[97mWhat is the magnitude of the final sum?[0m 210 211