From: Neil Smith Date: Mon, 11 Dec 2023 19:52:04 +0000 (+0000) Subject: Simplified day 9 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=f34155953a50dbc3196c1148ebc52dfadf9f5841;p=advent-of-code-23.git Simplified day 9 --- diff --git a/advent-of-code23.cabal b/advent-of-code23.cabal index 7b07a89..10fd7c4 100644 --- a/advent-of-code23.cabal +++ b/advent-of-code23.cabal @@ -154,6 +154,9 @@ executable advent08 executable advent09 import: common-extensions, build-directives main-is: advent09/Main.hs +executable advent09c + import: common-extensions, build-directives + main-is: advent09/MainComplicated.hs executable advent10 import: common-extensions, build-directives diff --git a/advent09/Main.hs b/advent09/Main.hs index 9352f5d..b232293 100644 --- a/advent09/Main.hs +++ b/advent09/Main.hs @@ -2,8 +2,9 @@ import AoC import Data.List +import Prelude hiding (seq) -newtype Sequence = Sequence [[Int]] deriving (Show, Eq) +type Sequence = [[Int]] main :: IO () main = @@ -11,12 +12,12 @@ main = text <- readFile dataFileName let histories = readInput text -- print histories - let seqs = fmap (expand . Sequence . pure) histories + let seqs = fmap expand histories -- print seqs -- let seqs' = fmap extend seqs -- print seqs' print $ part1 seqs - let rseqs = fmap (expand . Sequence . pure . reverse) histories + let rseqs = fmap (expand . reverse) histories print $ part1 rseqs part1 :: [Sequence] -> Int @@ -26,20 +27,21 @@ part1 = sum . fmap (evaluate . extend) readInput :: String -> [[Int]] readInput = fmap (fmap read . words) . lines -expand :: Sequence -> Sequence -expand (Sequence xss) - | all (== 0) $ last xss = Sequence xss - | otherwise = expand $ Sequence $ xss ++ [differences $ last xss] +expand :: [Int] -> Sequence +expand seq = unfoldr go seq + where go xs + | all (== 0) xs = Nothing + | otherwise = Just (xs, differences xs) differences :: [Int] -> [Int] differences xs = zipWith (-) (tail xs) xs extend :: Sequence -> Sequence -extend (Sequence xss) = Sequence $ fst $ foldr extendRow ([], 0) xss +extend = fst . foldr extendRow ([], 0) extendRow :: [Int] -> ([[Int]], Int) -> ([[Int]], Int) extendRow row (seq, n) = ((row ++ [n']) : seq, n') where n' = last row + n evaluate :: Sequence -> Int -evaluate (Sequence xss) = last $ head xss +evaluate = last . head diff --git a/advent09/MainComplicated.hs b/advent09/MainComplicated.hs new file mode 100644 index 0000000..9352f5d --- /dev/null +++ b/advent09/MainComplicated.hs @@ -0,0 +1,45 @@ +-- Writeup at https://work.njae.me.uk/2023/12/09/advent-of-code-2023-day-09/ + +import AoC +import Data.List + +newtype Sequence = Sequence [[Int]] deriving (Show, Eq) + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- readFile dataFileName + let histories = readInput text + -- print histories + let seqs = fmap (expand . Sequence . pure) histories + -- print seqs + -- let seqs' = fmap extend seqs + -- print seqs' + print $ part1 seqs + let rseqs = fmap (expand . Sequence . pure . reverse) histories + print $ part1 rseqs + +part1 :: [Sequence] -> Int +part1 = sum . fmap (evaluate . extend) + + +readInput :: String -> [[Int]] +readInput = fmap (fmap read . words) . lines + +expand :: Sequence -> Sequence +expand (Sequence xss) + | all (== 0) $ last xss = Sequence xss + | otherwise = expand $ Sequence $ xss ++ [differences $ last xss] + +differences :: [Int] -> [Int] +differences xs = zipWith (-) (tail xs) xs + +extend :: Sequence -> Sequence +extend (Sequence xss) = Sequence $ fst $ foldr extendRow ([], 0) xss + +extendRow :: [Int] -> ([[Int]], Int) -> ([[Int]], Int) +extendRow row (seq, n) = ((row ++ [n']) : seq, n') + where n' = last row + n + +evaluate :: Sequence -> Int +evaluate (Sequence xss) = last $ head xss