1 {-# LANGUAGE NegativeLiterals #-}
3 module Main(main) where
5 import Text.Parsec hiding (State)
6 -- import Text.ParserCombinators.Parsec.Number
12 text <- readFile "data/advent01.txt"
13 let instructions = successfulParse $ parseIline text
17 part1 :: [Int] -> IO ()
18 part1 instructions = do
19 print $ sum instructions
21 part2 :: [Int] -> IO ()
22 part2 instructions = do
23 print $ length $ takeWhile (>= 0) $ scanl (+) 0 instructions
27 -- instructionFile = instructionLine `endBy` newline
28 instructionLine = many (up <|> down)
31 up = char '(' *> pure 1
32 down = char ')' *> pure -1
34 -- parseIfile :: String -> Either ParseError [[Int]]
35 -- parseIfile input = parse instructionFile "(unknown)" input
37 parseIline :: String -> Either ParseError [Int]
38 parseIline input = parse instructionLine "(unknown)" input
40 successfulParse :: Either ParseError [a] -> [a]
41 successfulParse (Left _) = []
42 successfulParse (Right a) = a