From 5d41de69ff9389aadabdb1213b36750a02772e4e Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 19 Dec 2021 23:05:32 +0000 Subject: [PATCH] Done day 18 --- advent-of-code21.cabal | 5 + advent17/Main.hs | 2 +- advent18/Main.hs | 163 ++++++++++++++++++++++++ data/advent18.txt | 100 +++++++++++++++ data/advent18a.txt | 10 ++ data/advent18b.txt | 10 ++ problems/day18.html | 275 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 564 insertions(+), 1 deletion(-) create mode 100644 advent18/Main.hs create mode 100644 data/advent18.txt create mode 100644 data/advent18a.txt create mode 100644 data/advent18b.txt create mode 100644 problems/day18.html diff --git a/advent-of-code21.cabal b/advent-of-code21.cabal index 8652323..3154f15 100644 --- a/advent-of-code21.cabal +++ b/advent-of-code21.cabal @@ -191,3 +191,8 @@ executable advent17 import: common-extensions, build-directives main-is: advent17/Main.hs build-depends: linear, text, attoparsec, lens + +executable advent18 + import: common-extensions, build-directives + main-is: advent18/Main.hs + build-depends: text, attoparsec \ No newline at end of file diff --git a/advent17/Main.hs b/advent17/Main.hs index b6a4dab..2d1cf6b 100644 --- a/advent17/Main.hs +++ b/advent17/Main.hs @@ -44,7 +44,7 @@ findYMax (V2 _ y, _) = y' * (y' - 1) `div` 2 step :: Probe -> Probe step probe = probe & pos .~ (probe ^. pos ^+^ probe ^. vel) & vel .~ vel' where vel' = V2 (max 0 (vx - 1)) (vy - 1) - V2 vx vy = _vel + V2 vx vy = probe ^. vel -- v = probe ^. vel -- vel' = v & _x .~ (max 0 ((v ^. _x) - 1)) & _y .~ ((v ^. _y) - 1) diff --git a/advent18/Main.hs b/advent18/Main.hs new file mode 100644 index 0000000..0eb9ac8 --- /dev/null +++ b/advent18/Main.hs @@ -0,0 +1,163 @@ +-- Writeup at https://work.njae.me.uk/2021/12/19/advent-of-code-2021-day-17/ + +import Data.Text () +import qualified Data.Text.IO as TIO +import Data.Attoparsec.Text +import Control.Applicative +import Data.Maybe +import Data.List + +data Tree = Pair Tree Tree | Leaf Int + deriving (Show, Eq) + +data Cxt = Top | L Cxt Tree | R Tree Cxt + deriving (Show, Eq) + +-- type Context = [(Direction, Tree)] +-- data Direction = Lft | Rght +-- deriving (Show, Eq) + +type Loc = (Tree, Cxt) + +main :: IO () +main = + do text <- TIO.readFile "data/advent18.txt" + let numbers = successfulParse text + print $ part1 numbers + print $ part2 numbers + +part1 numbers = magnitude total + where total = foldl1' snailAdd numbers + +part2 numbers = maximum [ magnitude $ snailAdd a b + | a <- numbers, b <- numbers] + +magnitude :: Tree -> Int +magnitude (Leaf n) = n +magnitude (Pair a b) = 3 * (magnitude a) + 2 * (magnitude b) + + +left :: Loc -> Loc +left (Pair l r, c) = (l, L c r) + +right :: Loc -> Loc +right (Pair l r, c) = (r, R l c) + +top :: Tree -> Loc +top t = (t, Top) + +up :: Loc -> Loc +up (t, Top) = (t, Top) +up (t, L c r) = (Pair t r, c) +up (t, R l c) = (Pair l t, c) + +upmost :: Loc -> Loc +upmost l@(t, Top) = l +upmost l = upmost (up l) + +modify :: Loc -> (Tree -> Tree) -> Loc +modify (t, c) f = (f t, c) + + +explode :: Tree -> Maybe Tree +explode num = + case mp0 of + Nothing -> Nothing + Just _ -> Just num1 + where + mp0 = pairAtDepth 4 num + p0 = fromJust mp0 + ((Pair (Leaf nl) (Leaf nr)), _) = p0 + p1 = case rightmostOnLeft p0 of + Nothing -> p0 + Just leftReg -> modify leftReg (\(Leaf n) -> Leaf (n + nl)) + p2 = case pairAtDepthC 4 (upmost p1) >>= leftmostOnRight of + Nothing -> p1 + Just rightReg -> modify rightReg (\(Leaf n) -> Leaf (n + nr)) + p3 = case pairAtDepthC 4 (upmost p2) of + Nothing -> p2 + Just centrePair -> modify centrePair (\_ -> Leaf 0) + (num1, _) = upmost p3 + +pairAtDepth :: Int -> Tree -> Maybe Loc +pairAtDepth n t = pairAtDepthC n (top t) + +pairAtDepthC :: Int -> Loc -> Maybe Loc +pairAtDepthC _ (Leaf _, _) = Nothing +pairAtDepthC 0 t@(Pair _ _, _) = Just t +pairAtDepthC n t@(Pair _ _, _) = + pairAtDepthC (n - 1) (left t) <|> pairAtDepthC (n - 1) (right t) + +rightmostOnLeft :: Loc -> Maybe Loc +rightmostOnLeft (_, Top) = Nothing +rightmostOnLeft t@(_, L c r) = rightmostOnLeft $ up t +rightmostOnLeft t@(_, R l c) = Just $ rightmostNum $ left $ up t + +rightmostNum :: Loc -> Loc +rightmostNum t@(Leaf _, _) = t +rightmostNum t@(Pair _ _, _) = rightmostNum $ right t + +leftmostOnRight :: Loc -> Maybe Loc +leftmostOnRight (_, Top) = Nothing +leftmostOnRight t@(_, R l c) = leftmostOnRight $ up t +leftmostOnRight t@(_, L c r) = Just $ leftmostNum $ right $ up t + +leftmostNum :: Loc -> Loc +leftmostNum t@(Leaf _, _) = t +leftmostNum t@(Pair _ _, _) = leftmostNum $ left t + +split :: Tree -> Maybe Tree +split num = + case mn0 of + Nothing -> Nothing + Just _ -> Just num1 + where + mn0 = splittable num + n0 = fromJust mn0 + ((Leaf sn), _) = n0 + ln = sn `div` 2 + rn = ln + sn `mod` 2 + n1 = modify n0 (\_ -> Pair (Leaf ln) (Leaf rn)) + (num1, _) = upmost n1 + +splittable :: Tree -> Maybe Loc +splittable t = splittableC (top t) + +splittableC :: Loc -> Maybe Loc +splittableC t@(Leaf n, _) + | n >= 10 = Just t + | otherwise = Nothing +splittableC t@(Pair _ _, _) = splittableC (left t) <|> splittableC (right t) + + +reduce :: Tree -> Tree +reduce num = splt + where + expl = case (explode num) of + Nothing -> num + Just eres -> reduce eres + splt = case (split expl) of + Nothing -> expl + Just sres -> reduce sres + +snailAdd :: Tree -> Tree -> Tree +snailAdd a b = reduce $ Pair a b + + +-- Parse the input file + +sfNumbersP = sfNumberP `sepBy` endOfLine + +sfNumberP = regularP <|> pairP + +regularP = Leaf <$> decimal +pairP = Pair <$> ("[" *> sfNumberP) <*> ("," *> sfNumberP) <* "]" + +-- successfulParse :: Text -> (Integer, [Maybe Integer]) +successfulParse input = + case parseOnly sfNumbersP input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right numbers -> numbers + + + diff --git a/data/advent18.txt b/data/advent18.txt new file mode 100644 index 0000000..3ebba1e --- /dev/null +++ b/data/advent18.txt @@ -0,0 +1,100 @@ +[[2,[[1,1],9]],[[0,[3,0]],[[1,6],[4,2]]]] +[[8,[0,5]],[[[9,9],0],[[2,9],2]]] +[[[[6,4],[5,8]],[[0,9],[6,5]]],[[5,1],4]] +[[[9,[2,8]],[[0,2],[8,3]]],[[[5,6],[5,8]],[[4,8],2]]] +[[0,[[0,1],[6,0]]],[[[6,4],1],[8,6]]] +[[[[8,5],6],8],[[[9,1],[0,6]],[4,[2,4]]]] +[7,[[4,3],[8,5]]] +[[8,[1,[3,4]]],[[3,8],[0,1]]] +[[[1,1],[[2,1],[0,3]]],[[7,[1,8]],[[3,8],[5,2]]]] +[[2,[[4,6],[6,2]]],[[0,5],[3,7]]] +[[[[9,8],[4,6]],[7,[9,1]]],[[[8,7],[4,7]],[[6,6],[8,1]]]] +[[[2,[5,1]],[[0,4],3]],[[9,7],[[0,2],0]]] +[[[[5,0],2],5],[[3,[5,8]],[5,[8,9]]]] +[[6,[3,6]],[[[2,7],6],[[6,0],4]]] +[[8,8],7] +[[[[7,9],3],8],[[0,[1,7]],[[3,2],[4,5]]]] +[[[1,1],[7,2]],[3,[4,[6,4]]]] +[[[9,[6,6]],[[4,8],[1,3]]],[[[4,7],8],[[5,2],[3,8]]]] +[[[6,[6,7]],[3,4]],5] +[[[[0,0],2],9],[[[2,1],1],[5,[4,7]]]] +[[[2,[9,8]],[5,8]],[[[3,4],6],[5,0]]] +[[[7,[9,4]],[7,[7,2]]],[[1,[9,6]],1]] +[[[[9,1],1],[4,[2,6]]],3] +[[0,[8,[3,4]]],[8,[9,8]]] +[[[1,6],[6,7]],[[[0,4],1],7]] +[[6,[5,[0,0]]],[7,[[5,4],1]]] +[[2,[[9,5],[9,1]]],[[3,0],4]] +[[[5,7],[[1,0],[3,5]]],[4,[5,[4,0]]]] +[[3,3],[2,2]] +[[[[6,2],[1,7]],[1,7]],[[[6,7],6],9]] +[[[[9,8],[8,8]],[2,1]],[[8,4],8]] +[[[[1,4],1],[2,0]],[4,[[0,5],5]]] +[[[7,[6,0]],[[7,3],1]],9] +[[[[2,4],0],[[6,9],8]],[[3,[0,9]],[[4,4],[5,4]]]] +[[7,3],[0,[2,[7,2]]]] +[[[[8,8],5],9],[[8,6],6]] +[[[[9,5],7],9],0] +[[[1,4],8],[[7,[5,3]],[[6,4],6]]] +[[9,[[9,3],[3,7]]],[[[6,9],1],[[2,3],[4,4]]]] +[[4,[9,2]],[3,4]] +[[1,[[0,9],2]],[1,[1,[8,7]]]] +[[[4,1],8],[9,[9,[2,9]]]] +[[[[7,9],[9,7]],8],[[[3,0],5],[[7,8],[3,1]]]] +[[[[9,4],[9,9]],[[9,5],[8,9]]],[[2,[7,4]],[[4,6],6]]] +[[[[8,7],1],[6,8]],[[4,2],5]] +[7,[3,[3,3]]] +[[[4,9],[0,2]],[[[4,2],9],[[5,8],6]]] +[[[[1,3],1],[[7,5],[4,0]]],[[[6,3],4],[[1,2],8]]] +[[[[3,2],2],[4,7]],[[[5,6],[6,3]],3]] +[[[[4,0],6],[4,2]],[7,5]] +[[[[9,5],[2,0]],[[6,8],[0,9]]],[[[7,4],[3,6]],1]] +[[[4,[9,3]],[[9,4],8]],[[6,[1,2]],2]] +[[[[4,1],[1,1]],[[4,8],9]],[[[1,0],[0,3]],2]] +[[[3,[3,8]],[[0,6],7]],[[2,5],9]] +[[[0,[6,8]],[[2,7],[4,1]]],6] +[[6,3],0] +[[[3,[7,1]],[3,[2,0]]],[[[3,5],9],[[5,2],[7,8]]]] +[[7,8],[1,[[7,1],5]]] +[[[9,[8,9]],2],[9,[[8,8],4]]] +[[[8,[5,8]],[[9,1],[6,0]]],[[[9,1],[4,7]],8]] +[5,[[[4,9],7],[[6,0],[9,0]]]] +[[[[8,8],[6,7]],[[1,0],6]],[[5,[2,8]],[[8,0],[3,7]]]] +[[0,[6,6]],[[0,1],[3,[9,2]]]] +[[1,[0,[8,1]]],[[0,[0,0]],[8,[0,0]]]] +[[[4,[1,4]],[8,[9,5]]],7] +[7,[[[0,0],[4,3]],8]] +[[[9,1],[[7,5],[9,2]]],[5,[9,0]]] +[[[[2,0],9],[8,[3,0]]],[[9,8],[4,[0,7]]]] +[4,[5,[5,[0,3]]]] +[[6,[[6,9],8]],[1,[0,[6,0]]]] +[[7,[4,3]],[[0,6],[[5,2],[6,9]]]] +[[[[7,2],[4,6]],[[5,0],9]],6] +[[[0,1],[0,2]],[0,[5,2]]] +[[[[5,0],[5,4]],[[5,9],[9,9]]],[2,[[3,0],[8,1]]]] +[[[[9,2],[2,9]],[[5,5],2]],[[1,3],[[3,6],[1,8]]]] +[[0,[2,4]],[[[6,9],1],[[7,9],[9,8]]]] +[[[[2,1],1],[7,3]],[4,[[1,2],[2,6]]]] +[[[6,[0,1]],[[6,4],[4,2]]],[1,[[0,0],[9,7]]]] +[[[[9,2],3],[9,8]],[[6,5],[7,[1,7]]]] +[[3,9],7] +[[[6,9],[[0,2],0]],[[[8,6],2],9]] +[[[[2,2],2],[[6,7],7]],[[0,3],9]] +[[[7,[2,7]],3],4] +[[[[1,9],6],[0,7]],[[[2,2],1],2]] +[9,9] +[0,[9,[[4,1],1]]] +[[[[7,6],1],2],[[[6,9],[9,1]],0]] +[[[[4,3],[4,2]],3],[[5,[6,5]],[[2,6],0]]] +[[[0,[5,1]],[6,[1,4]]],[5,[[8,1],3]]] +[6,[9,6]] +[[8,[9,[6,8]]],[[4,9],[[2,4],[7,1]]]] +[[5,[[9,9],[3,3]]],[[[9,8],[5,0]],6]] +[[6,7],1] +[1,[4,[[9,6],0]]] +[[[[9,8],[7,8]],[5,[4,6]]],[[[5,9],6],[[4,6],4]]] +[[[2,7],4],[[[0,3],0],[[7,4],[7,4]]]] +[7,[0,4]] +[1,[3,2]] +[[3,0],8] +[[[3,2],5],8] \ No newline at end of file diff --git a/data/advent18a.txt b/data/advent18a.txt new file mode 100644 index 0000000..20d54d0 --- /dev/null +++ b/data/advent18a.txt @@ -0,0 +1,10 @@ +[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]] +[7,[[[3,7],[4,3]],[[6,3],[8,8]]]] +[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]] +[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]] +[7,[5,[[3,8],[1,4]]]] +[[2,[2,2]],[8,[8,1]]] +[2,9] +[1,[[[9,3],9],[[9,0],[0,7]]]] +[[[5,[7,4]],7],1] +[[[[4,2],2],6],[8,7]] \ No newline at end of file diff --git a/data/advent18b.txt b/data/advent18b.txt new file mode 100644 index 0000000..2efedbf --- /dev/null +++ b/data/advent18b.txt @@ -0,0 +1,10 @@ +[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]] +[[[5,[2,8]],4],[5,[[9,9],0]]] +[6,[[[6,2],[5,6]],[[7,6],[4,7]]]] +[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]] +[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]] +[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]] +[[[[5,4],[7,7]],8],[[8,3],8]] +[[9,3],[[9,9],[6,[4,9]]]] +[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] +[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]] \ No newline at end of file diff --git a/problems/day18.html b/problems/day18.html new file mode 100644 index 0000000..d172405 --- /dev/null +++ b/problems/day18.html @@ -0,0 +1,275 @@ + + + + +Day 18 - Advent of Code 2021 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 36*

   sub y{2021}

+ + + +
+ +

--- Day 18: Snailfish ---

You descend into the ocean trench and encounter some snailfish. They say they saw the sleigh keys! They'll even tell you which direction the keys went if you help one of the smaller snailfish with his math homework.

+

Snailfish numbers aren't like regular numbers. Instead, every snailfish number is a pair - an ordered list of two elements. Each element of the pair can be either a regular number or another pair.

+

Pairs are written as [x,y], where x and y are the elements within the pair. Here are some example snailfish numbers, one snailfish number per line:

+
[1,2]
+[[1,2],3]
+[9,[8,7]]
+[[1,9],[8,5]]
+[[[[1,2],[3,4]],[[5,6],[7,8]]],9]
+[[[9,[3,8]],[[0,9],6]],[[[3,7],[4,9]],3]]
+[[[[1,3],[5,3]],[[1,3],[8,7]]],[[[4,9],[6,9]],[[8,2],[7,3]]]]
+
+

This snailfish homework is about addition. To add two snailfish numbers, form a pair from the left and right parameters of the addition operator. For example, [1,2] + [[3,4],5] becomes [[1,2],[[3,4],5]].

+

There's only one problem: snailfish numbers must always be reduced, and the process of adding two snailfish numbers can result in snailfish numbers that need to be reduced.

+

To reduce a snailfish number, you must repeatedly do the first action in this list that applies to the snailfish number:

+
    +
  • If any pair is nested inside four pairs, the leftmost such pair explodes.
  • +
  • If any regular number is 10 or greater, the leftmost such regular number splits.
  • +
+

Once no action in the above list applies, the snailfish number is reduced.

+

During reduction, at most one action applies, after which the process returns to the top of the list of actions. For example, if split produces a pair that meets the explode criteria, that pair explodes before other splits occur.

+

To explode a pair, the pair's left value is added to the first regular number to the left of the exploding pair (if any), and the pair's right value is added to the first regular number to the right of the exploding pair (if any). Exploding pairs will always consist of two regular numbers. Then, the entire exploding pair is replaced with the regular number 0.

+

Here are some examples of a single explode action:

+
    +
  • [[[[[9,8],1],2],3],4] becomes [[[[0,9],2],3],4] (the 9 has no regular number to its left, so it is not added to any regular number).
  • +
  • [7,[6,[5,[4,[3,2]]]]] becomes [7,[6,[5,[7,0]]]] (the 2 has no regular number to its right, and so it is not added to any regular number).
  • +
  • [[6,[5,[4,[3,2]]]],1] becomes [[6,[5,[7,0]]],3].
  • +
  • [[3,[2,[1,[7,3]]]],[6,[5,[4,[3,2]]]]] becomes [[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]] (the pair [3,2] is unaffected because the pair [7,3] is further to the left; [3,2] would explode on the next action).
  • +
  • [[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]] becomes [[3,[2,[8,0]]],[9,[5,[7,0]]]].
  • +
+

To split a regular number, replace it with a pair; the left element of the pair should be the regular number divided by two and rounded down, while the right element of the pair should be the regular number divided by two and rounded up. For example, 10 becomes [5,5], 11 becomes [5,6], 12 becomes [6,6], and so on.

+

Here is the process of finding the reduced result of [[[[4,3],4],4],[7,[[8,4],9]]] + [1,1]:

+
after addition: [[[[[4,3],4],4],[7,[[8,4],9]]],[1,1]]
+after explode:  [[[[0,7],4],[7,[[8,4],9]]],[1,1]]
+after explode:  [[[[0,7],4],[15,[0,13]]],[1,1]]
+after split:    [[[[0,7],4],[[7,8],[0,13]]],[1,1]]
+after split:    [[[[0,7],4],[[7,8],[0,[6,7]]]],[1,1]]
+after explode:  [[[[0,7],4],[[7,8],[6,0]]],[8,1]]
+
+

Once no reduce actions apply, the snailfish number that remains is the actual result of the addition operation: [[[[0,7],4],[[7,8],[6,0]]],[8,1]].

+

The homework assignment involves adding up a list of snailfish numbers (your puzzle input). The snailfish numbers are each listed on a separate line. Add the first snailfish number and the second, then add that result and the third, then add that result and the fourth, and so on until all numbers in the list have been used once.

+

For example, the final sum of this list is [[[[1,1],[2,2]],[3,3]],[4,4]]:

+
[1,1]
+[2,2]
+[3,3]
+[4,4]
+
+

The final sum of this list is [[[[3,0],[5,3]],[4,4]],[5,5]]:

+
[1,1]
+[2,2]
+[3,3]
+[4,4]
+[5,5]
+
+

The final sum of this list is [[[[5,0],[7,4]],[5,5]],[6,6]]:

+
[1,1]
+[2,2]
+[3,3]
+[4,4]
+[5,5]
+[6,6]
+
+

Here's a slightly larger example:

+
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
+[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
+[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
+[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
+[7,[5,[[3,8],[1,4]]]]
+[[2,[2,2]],[8,[8,1]]]
+[2,9]
+[1,[[[9,3],9],[[9,0],[0,7]]]]
+[[[5,[7,4]],7],1]
+[[[[4,2],2],6],[8,7]]
+
+

The final sum [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] is found after adding up the above snailfish numbers:

+
  [[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
++ [7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
+= [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
+
+  [[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]
++ [[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
+= [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
+
+  [[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]
++ [[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
+= [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
+
+  [[[[7,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]
++ [7,[5,[[3,8],[1,4]]]]
+= [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
+
+  [[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]
++ [[2,[2,2]],[8,[8,1]]]
+= [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
+
+  [[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]
++ [2,9]
+= [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
+
+  [[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]
++ [1,[[[9,3],9],[[9,0],[0,7]]]]
+= [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
+
+  [[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]
++ [[[5,[7,4]],7],1]
+= [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
+
+  [[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]
++ [[[[4,2],2],6],[8,7]]
+= [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]
+
+

To check whether it's the right answer, the snailfish teacher only checks the magnitude of the final sum. The magnitude of a pair is 3 times the magnitude of its left element plus 2 times the magnitude of its right element. The magnitude of a regular number is just that number.

+

For example, the magnitude of [9,1] is 3*9 + 2*1 = 29; the magnitude of [1,9] is 3*1 + 2*9 = 21. Magnitude calculations are recursive: the magnitude of [[9,1],[1,9]] is 3*29 + 2*21 = 129.

+

Here are a few more magnitude examples:

+
    +
  • [[1,2],[[3,4],5]] becomes 143.
  • +
  • [[[[0,7],4],[[7,8],[6,0]]],[8,1]] becomes 1384.
  • +
  • [[[[1,1],[2,2]],[3,3]],[4,4]] becomes 445.
  • +
  • [[[[3,0],[5,3]],[4,4]],[5,5]] becomes 791.
  • +
  • [[[[5,0],[7,4]],[5,5]],[6,6]] becomes 1137.
  • +
  • [[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]] becomes 3488.
  • +
+

So, given this example homework assignment:

+
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
+[[[5,[2,8]],4],[5,[[9,9],0]]]
+[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
+[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
+[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
+[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
+[[[[5,4],[7,7]],8],[[8,3],8]]
+[[9,3],[[9,9],[6,[4,9]]]]
+[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
+[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
+
+

The final sum is:

+
[[[[6,6],[7,6]],[[7,7],[7,0]]],[[[7,7],[7,7]],[[7,8],[9,9]]]]
+

The magnitude of this final sum is 4140.

+

Add up all of the snailfish numbers from the homework assignment in the order they appear. What is the magnitude of the final sum?

+
+

Your puzzle answer was 2501.

--- Part Two ---

You notice a second question on the back of the homework assignment:

+

What is the largest magnitude you can get from adding only two of the snailfish numbers?

+

Note that snailfish addition is not commutative - that is, x + y and y + x can produce different results.

+

Again considering the last example homework assignment above:

+
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
+[[[5,[2,8]],4],[5,[[9,9],0]]]
+[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
+[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
+[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
+[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
+[[[[5,4],[7,7]],8],[[8,3],8]]
+[[9,3],[[9,9],[6,[4,9]]]]
+[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
+[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]
+
+

The largest magnitude of the sum of any two snailfish numbers in this list is 3993. This is the magnitude of [[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] + [[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]], which reduces to [[[[7,8],[6,6]],[[6,0],[7,7]]],[[[7,8],[8,8]],[[7,9],[0,6]]]].

+

What is the largest magnitude of any sum of two different snailfish numbers from the homework assignment?

+
+

Your puzzle answer was 4935.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1