Skip to content

Instantly share code, notes, and snippets.

@bryanwoods
Last active December 2, 2019 11:46
Show Gist options
  • Save bryanwoods/d6fcaa17a1f8b3be0bb04bf4f44505c2 to your computer and use it in GitHub Desktop.
Save bryanwoods/d6fcaa17a1f8b3be0bb04bf4f44505c2 to your computer and use it in GitHub Desktop.
Advent of Code 2019
module Main where
elfFuel :: Int -> Int
elfFuel mass = mass `div` 3 - 2
cumulativeElfFuel :: Int -> Int
cumulativeElfFuel mass
| elfFuel mass <= 0 = 0
| mass > 0 = (elfFuel mass) + cumulativeElfFuel (elfFuel mass)
appliedSum :: Num c => (a -> c) -> [a] -> c
appliedSum f vs = sum $ map f vs
intFromLine :: Read b => (b -> Int) -> String -> Int
intFromLine v = (v . read :: String -> Int)
part1 :: [String] -> Int
part1 masses = appliedSum (intFromLine $ elfFuel) $ masses
part2 :: [String] -> Int
part2 masses = appliedSum (intFromLine $ cumulativeElfFuel) $ masses
main = do
input <- readFile "input"
let masses = lines input
print (part1 masses)
print (part2 masses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment