Tweaked
[advent-of-code-17.git] / adventofcode1701 / app / advent01.hs
1 {-# LANGUAGE NegativeLiterals #-}
2
3 module Main(main) where
4
5 import Text.Parsec hiding (State)
6 -- import Text.ParserCombinators.Parsec.Number
7
8
9
10 main :: IO ()
11 main = do
12 text <- readFile "data/advent01.txt"
13 let instructions = successfulParse $ parseIline text
14 part1 instructions
15 part2 instructions
16
17 part1 :: [Int] -> IO ()
18 part1 instructions = do
19 print $ sum instructions
20
21 part2 :: [Int] -> IO ()
22 part2 instructions = do
23 print $ length $ takeWhile (>= 0) $ scanl (+) 0 instructions
24
25
26
27 -- instructionFile = instructionLine `endBy` newline
28 instructionLine = many (up <|> down)
29
30
31 up = char '(' *> pure 1
32 down = char ')' *> pure -1
33
34 -- parseIfile :: String -> Either ParseError [[Int]]
35 -- parseIfile input = parse instructionFile "(unknown)" input
36
37 parseIline :: String -> Either ParseError [Int]
38 parseIline input = parse instructionLine "(unknown)" input
39
40 successfulParse :: Either ParseError [a] -> [a]
41 successfulParse (Left _) = []
42 successfulParse (Right a) = a