X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent18%2FMain.hs;h=d9638c3f896d5ce36fdf36bb0727ee6b6529b682;hb=e2b15781f674220586e860fb9a85b6ad0f278fad;hp=0eb9ac881be8c2393c70709ad1ca3648e4ef43f5;hpb=5d41de69ff9389aadabdb1213b36750a02772e4e;p=advent-of-code-21.git

diff --git a/advent18/Main.hs b/advent18/Main.hs
index 0eb9ac8..d9638c3 100644
--- a/advent18/Main.hs
+++ b/advent18/Main.hs
@@ -1,4 +1,4 @@
--- 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
@@ -8,7 +8,11 @@ import Data.Maybe
 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)
@@ -129,16 +133,10 @@ splittableC t@(Leaf n, _)
   | 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