1 import Data.Text (Text)
2 import qualified Data.Text.IO as TIO
4 import Data.Void (Void)
7 import Text.Megaparsec.Char
8 import qualified Text.Megaparsec.Char.Lexer as L
9 import qualified Control.Applicative as CA
14 text <- TIO.readFile "data/advent01.txt"
15 let changes = successfulParse text
21 part1 = sum . map fuelRequired
24 part2 = sum . map fuelForFuel
27 fuelRequired :: Int -> Int
28 fuelRequired m = (m `div` 3) - 2
30 fuelForFuel :: Int -> Int
31 fuelForFuel = sum . takeWhile (> 0) . drop 1 . iterate fuelRequired
34 -- Parse the input file
35 type Parser = Parsec Void Text
38 sc = L.space (skipSome spaceChar) CA.empty CA.empty
39 -- sc = L.space (skipSome (char ' ')) CA.empty CA.empty
43 integer = lexeme L.decimal
44 -- signedInteger = L.signed sc integer
46 moduleP = many integer
48 successfulParse :: Text -> [Int]
49 successfulParse input =
50 case parse moduleP "input" input of
51 Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
52 Right changes -> changes