Advent of Code, Day 1
In the first day of Advent of Code, the task is to first calculate the fuel needed for some mass, and then treat that fuel as mass, and calculate the fuel needed for that and so on, then add it together.
Nothing special going on in this code, so I’ll just post it:
module Day1 where
fuelForMass :: Integer -> Integer
fuelForMass mass = (mass `div` 3) - 2
fuelForMassRec :: Integer -> Integer
fuelForMassRec mass = go mass 0
where
go 0 acc = acc
go mass acc =
let
fuel = fuelForMass mass
in
if fuel <= 0
then acc
else go fuel (acc + fuel)
part1 :: IO Integer
part1 = do
input <- readFile "../input/day1.txt"
let fuel = (fuelForMass . read <$> lines input) :: [Integer]
return $ foldr (+) 0 fuel
part2 :: IO Integer
part2 = do
input <- readFile "../input/day1.txt"
let fuels = (fuelForMassRec . read <$> lines input) :: [Integer]
return $ foldr (+) 0 fuels