-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay01.hs
41 lines (28 loc) · 772 Bytes
/
Day01.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Main where
import Utilities
-- Input processing
type Input = [Int]
parse :: String -> Input
parse = map read . lines
-- Part One
fuel :: Int -> Int
fuel m = m `div` 3 - 2
solve1 :: Input -> Int
solve1 = sum . map fuel
tests1 :: [(Int, Int)]
tests1 = [(12, 2), (14, 2), (1969, 654), (100756, 33583)]
-- Part Two
all_fuel :: Int -> Int
all_fuel = sum . takeWhile (>0) . tail . iterate fuel
solve2 :: Input -> Int
solve2 = sum . map all_fuel
tests2 :: [(Int, Int)]
tests2 = [(14, 2), (1969, 966), (100756, 50346)]
main :: IO ()
main = do
s <- readFile "input/01.txt"
let input = parse s
putStr (unlines (failures "fuel" fuel tests1))
print (solve1 input)
putStr (unlines (failures "all_fuel" all_fuel tests2))
print (solve2 input)