From 6bbb33d72d3435fb15e9b30bd571c614ea76b971 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Sat, 9 Dec 2023 00:01:25 -0800 Subject: [PATCH] 2023 day 9 --- 2023/9/Main1.hs | 12 ++++++++++++ 2023/9/Main2.hs | 12 ++++++++++++ flake.nix | 1 + 3 files changed, 25 insertions(+) create mode 100644 2023/9/Main1.hs create mode 100644 2023/9/Main2.hs diff --git a/2023/9/Main1.hs b/2023/9/Main1.hs new file mode 100644 index 0000000..de0e191 --- /dev/null +++ b/2023/9/Main1.hs @@ -0,0 +1,12 @@ +import Data.List.Split (splitOn) + +solve :: [Int] -> Int +solve [] = 0 +solve xs@(x:_) = x + 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..08cd166 --- /dev/null +++ b/2023/9/Main2.hs @@ -0,0 +1,12 @@ +import Data.List.Split (splitOn) + +solve :: [Int] -> Int +solve [] = 0 +solve xs@(x:_) = x + solve (getDifferences xs) + where + getDifferences = zipWith (-) <*> tail + +main :: IO () +main = print . sum . map solveLine . lines =<< readFile "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