D1 Haskell: make cost calculation more readable

This commit is contained in:
kageru 2019-12-01 11:39:40 +01:00
parent cbec64cf35
commit 4df684d650
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -5,13 +5,16 @@ main :: IO ()
main = do main = do
content <- getContents content <- getContents
let input = map read (lines content) let input = map read (lines content)
printf "Part 1: %d\n" (fuel input) printf "Part 1: %d\n" (part1 input)
printf "Part 2: %d\n" (fuelrec input) printf "Part 2: %d\n" (part2 input)
fuel :: [Integer] -> Integer part1 :: [Integer] -> Integer
fuel xs = sum (map (subtract 2 . (`div` 3)) xs) part1 xs = sum (map cost xs)
fuelrec :: [Integer] -> Integer part2 :: [Integer] -> Integer
fuelrec xs = sum (map f xs) - sum xs where part2 xs = sum (map f xs) - sum xs where
f x | x > 0 = x + f ((subtract 2 . (`div` 3)) x) f x | x > 0 = x + f (cost x)
| otherwise = 0 | otherwise = 0
cost :: Integer -> Integer
cost x = div x 3 - 2