Skip to content

Commit

Permalink
Day_09(2023): solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheinxy committed Dec 9, 2023
1 parent 6e6c3ac commit 3a66bfe
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 2023/Day_09/Day_09.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Main where

import System.Environment

type Input = [[Int]]
type Output = Int

parseInput :: String -> Input
parseInput = map (map read . words) . lines

generateSubsequence :: [Int] -> [[Int]]
generateSubsequence = takeWhile (not . all (== 0)) . iterate getDiffs
where getDiffs l = zipWith (-) (tail l) l

partOne :: Input -> Output
partOne = sum . map (foldr ((+) . last) 0 . generateSubsequence)

partTwo :: Input -> Output
partTwo = sum . map (foldr ((-) . head) 0 . generateSubsequence)

compute :: Input -> String -> IO ()
compute input "parse" = print input
compute input "one" = print . partOne $ input
compute input "two" = print . partTwo $ input
compute input _ = error "Unknown part"

main = do
args <- getArgs
input <- parseInput <$> (readFile $ last args)
mapM (compute input) $ init args
1 change: 1 addition & 0 deletions 2023/Day_09/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Day 09
Loading

0 comments on commit 3a66bfe

Please sign in to comment.