never executed always true always false
1 {-|
2 Module: Y2015.D01
3 Description: Advent of Code Day 01 Solutions.
4 License: MIT
5 Maintainer: @tylerjl
6
7 Solutions to the day 01 set of problems for <adventofcode.com>.
8 -}
9 module Y2015.D01
10 ( level
11 , basement
12 ) where
13
14 import Data.Attoparsec.Text
15 import Data.List (foldl')
16 import Data.Text (Text)
17 import Data.Either.Utils
18 import Control.Applicative ((<|>))
19
20 -- |Parse day 1's input.
21 parse1 :: Text -> [Int]
22 parse1 = fromRight . parseOnly parser
23 where
24 parser = many1 levels <* skipMany endOfLine <* atEnd
25 levels = 1 <$ char '('
26 <|> (-1) <$ char ')'
27
28 -- |Find final level from list of elevator movements
29 level
30 :: Text -- ^ List of input open/close parens
31 -> Int -- ^ Final elevator level
32 level = foldl' (+) 0 . parse1
33
34 -- |Find position that arrives at level 0
35 basement
36 :: Text -- ^ List of input open/close parens
37 -> Maybe Int -- ^ Possible position in string that arrives at zero
38 basement = find 0 1 . parse1
39 where
40 find current idx (x:xs)
41 | current + x < 0 = Just idx
42 | otherwise = find (current + x) (idx + 1) xs
43 find _ _ [] = Nothing