diff --git a/2023/9/Main1.hs b/2023/9/Main1.hs new file mode 100644 index 0000000..c5f2075 --- /dev/null +++ b/2023/9/Main1.hs @@ -0,0 +1,12 @@ +import Data.List.Split (splitOn) + +solve :: [Int] -> Int +solve [] = 0 +solve xs = head xs + solve (getDifferences xs) + where + getDifferences = zipWith (-) <*> tail + +main :: IO () +main = print . sum . map solveLine . lines =<< readFile "data.txt" + where + solveLine = solve . reverse . map read . splitOn " " diff --git a/2023/9/Main2.hs b/2023/9/Main2.hs new file mode 100644 index 0000000..b9e41c3 --- /dev/null +++ b/2023/9/Main2.hs @@ -0,0 +1,12 @@ +import Data.List.Split (splitOn) + +solve :: [Int] -> Int +solve [] = 0 +solve xs = head xs + solve (getDifferences xs) + where + getDifferences = zipWith (-) <*> tail + +main :: IO () +main = print . sum . map solveLine . lines =<< readFile "test-data.txt" + where + solveLine = solve . map read . splitOn " " diff --git a/flake.nix b/flake.nix index e036f0e..456b248 100644 --- a/flake.nix +++ b/flake.nix @@ -32,6 +32,7 @@ (pkgs.ghc.withPackages (p: with p; [ containers + extra regex-compat split unordered-containers