From c158f59eff511f9ff41bd3a4947b955e31e50a42 Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 1 Dec 2019 10:01:47 +0100 Subject: [PATCH] Add day 1 in Haskell --- 2019/1/day1.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 2019/1/day1.hs diff --git a/2019/1/day1.hs b/2019/1/day1.hs new file mode 100644 index 0000000..255d7ab --- /dev/null +++ b/2019/1/day1.hs @@ -0,0 +1,17 @@ +import System.IO +import Text.Printf + +main :: IO () +main = do + content <- getContents + let input = map read (lines content) + printf "Part 1: %d\n" (fuel input) + printf "Part 2: %d\n" (fuelrec input) + +fuel :: [Int] -> Int +fuel xs = sum (map (subtract 2 . (`div` 3)) xs) + +fuelrec :: [Int] -> Int +fuelrec xs = sum (map f xs) - sum xs where + f x | x > 0 = x + f ((subtract 2 . (`div` 3)) x) + | otherwise = 0