Last active
December 8, 2019 14:51
-
-
Save k0001/c121f90a9faa6f1afad8cb50ad9e1e89 to your computer and use it in GitHub Desktop.
AdventOfCode2019
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 DerivingStrategies #-} | |
{-# LANGUAGE DerivingVia #-} | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
module Aoc1 where | |
import Data.Foldable | |
import Data.Monoid (Sum(..)) | |
import Data.Ratio | |
---- Excercise 1 | |
newtype Mass = Mass Integer | |
deriving stock (Eq, Ord, Show) | |
deriving Semigroup via (Sum Integer) | |
deriving Monoid via (Sum Integer) | |
newtype Fuel = Fuel Mass | |
deriving stock (Eq, Ord, Show) | |
deriving newtype Semigroup | |
deriving newtype Monoid | |
massCost :: Mass -> Fuel | |
massCost (Mass i) = Fuel (Mass (floor (i % 3) - 2)) | |
-- 3471229 | |
answer1 :: Fuel | |
answer1 = foldl' mappend mempty (fmap massCost input) | |
---- Exercise 2 | |
fuelCost :: Fuel -> Fuel | |
fuelCost (Fuel m0) = | |
case massCost m0 of | |
f1 | f1 > mempty -> mappend f1 (fuelCost f1) | |
| otherwise -> mempty | |
-- 5203967 | |
answer2 :: Fuel | |
answer2 = foldl' mappend mempty (fmap (fuelCost . Fuel) input) | |
input :: [Mass] | |
input = fmap Mass | |
[129192, | |
58561, | |
57267, | |
95382, | |
84995, | |
127372, | |
93598, | |
97264, | |
138550, | |
79327, | |
135661, | |
139468, | |
108860, | |
149642, | |
72123, | |
128333, | |
69002, | |
98450, | |
86267, | |
70171, | |
101333, | |
79822, | |
142539, | |
142743, | |
51371, | |
111381, | |
62073, | |
72210, | |
125168, | |
135952, | |
131060, | |
121842, | |
88234, | |
146774, | |
136571, | |
126719, | |
50644, | |
75696, | |
51195, | |
77171, | |
118052, | |
83691, | |
133779, | |
149814, | |
64847, | |
110697, | |
92695, | |
59453, | |
139517, | |
129487, | |
79271, | |
97896, | |
146987, | |
149822, | |
71866, | |
90797, | |
104732, | |
54997, | |
50139, | |
134115, | |
133017, | |
144979, | |
89428, | |
124750, | |
91833, | |
57252, | |
67195, | |
121624, | |
102706, | |
138245, | |
127700, | |
124098, | |
110382, | |
121557, | |
103613, | |
133576, | |
122801, | |
112306, | |
120203, | |
134696, | |
76129, | |
84576, | |
80854, | |
147237, | |
71025, | |
127513, | |
143631, | |
125090, | |
115698, | |
57979, | |
84880, | |
120177, | |
147389, | |
88380, | |
114688, | |
56355, | |
126265, | |
58220, | |
63523, | |
130179] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment