--- Writeup at https://work.njae.me.uk/2021/12/19/advent-of-code-2021-day-17/
+-- Writeup at https://work.njae.me.uk/2021/12/21/advent-of-code-2021-day-18/
import Data.Text ()
import qualified Data.Text.IO as TIO
import Data.List
data Tree = Pair Tree Tree | Leaf Int
- deriving (Show, Eq)
+ deriving (Eq)
+
+instance Show Tree where
+ show (Leaf n) = show n
+ show (Pair l r) = "[" ++ show l ++ "," ++ show r ++ "]"
data Cxt = Top | L Cxt Tree | R Tree Cxt
deriving (Show, Eq)
| 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
+reduce num = case explode num <|> split num of
+ Nothing -> num
+ Just num1 -> reduce num1
snailAdd :: Tree -> Tree -> Tree
snailAdd a b = reduce $ Pair a b