From: Neil Smith Date: Tue, 21 Dec 2021 09:18:30 +0000 (+0000) Subject: Simplified reduce X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=b13225f575276c1b26363f362cc4810ef9fbcead;p=advent-of-code-21.git Simplified reduce --- 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