advent-of-code/2019/01/day1.hs

21 lines
441 B
Haskell
Raw Permalink Normal View History

2019-12-01 10:01:47 +01:00
import System.IO
import Text.Printf
main :: IO ()
main = do
content <- getContents
let input = map read (lines content)
printf "Part 1: %d\n" (part1 input)
printf "Part 2: %d\n" (part2 input)
2019-12-01 10:01:47 +01:00
part1 :: [Integer] -> Integer
part1 xs = sum (map cost xs)
2019-12-01 10:01:47 +01:00
part2 :: [Integer] -> Integer
part2 xs = sum (map f xs) - sum xs where
f x | x > 0 = x + f (cost x)
2019-12-01 10:01:47 +01:00
| otherwise = 0
cost :: Integer -> Integer
cost x = div x 3 - 2