Created
February 17, 2020 09:45
-
-
Save tomwadeson/76a6a7b54a610fe8743cae1589c9368c to your computer and use it in GitHub Desktop.
Advent Of Code 2019, Day 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DerivingVia #-} | |
{-# LANGUAGE GeneralisedNewtypeDeriving #-} | |
module Day1 | |
( moduleMasses | |
, partOne | |
, partTwo | |
) | |
where | |
import Data.Coerce ( coerce ) | |
import Data.Foldable ( fold ) | |
import Data.Monoid ( Sum(..) ) | |
import Streaming ( Of | |
, Stream | |
) | |
import qualified Streaming.Prelude as S | |
newtype Mass = Mass Int | |
deriving newtype Read | |
newtype Fuel = Fuel Int | |
deriving stock Show | |
deriving Semigroup via (Sum Int) | |
deriving Monoid via (Sum Int) | |
fuel :: Mass -> Fuel | |
fuel (Mass m) = Fuel $ m `div` 3 - 2 | |
-- At this point, "fuel" doesn't look like a word anymore | |
fuelFuel :: Fuel -> Fuel | |
fuelFuel = fold . takeWhile (\(Fuel f) -> f > 0) . iterate (fuel . coerce) | |
moduleMasses :: Stream (Of Mass) IO () | |
moduleMasses = S.readLn | |
partOne :: Monad m => Stream (Of Mass) m () -> m Fuel | |
partOne = S.foldMap_ fuel | |
partTwo :: Monad m => Stream (Of Mass) m () -> m Fuel | |
partTwo = S.foldMap_ id . S.map (fuelFuel . fuel) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment