summary refs log tree commit diff
path: root/src/Day3.idr
blob: 5f14827593f3d8165ca095e69b71794649c84ab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Day3 

import Data.String.Parser
import Data.String

mul : Parser Integer
mul = (*) <$ string "mul(" <*> integer <* string "," <*> integer <* string ")"


partParser : Parser a -> String -> List a
partParser p str = case str of
                 "" => []
                 _ => case parse p str of
                           Left _ => partParser p (strSubstr 1 (strLength str) str)
                           Right (num, bla) => num :: (partParser p (strSubstr bla (strLength str) str))

part1 : String -> List Integer
part1 = partParser mul

export sol1 : String -> String
sol1 = show . sum . part1

dont : Parser (Either Bool Integer)
dont = Left <$> ((True <$ string "do()") <|> (False <$ string "don't()"))

mul2 : Parser (Either Bool Integer)
mul2 = (Right <$> mul) <|> dont

part2 : String -> List (Either Bool Integer)
part2 = partParser mul2

toggleSum : Bool -> List (Either Bool Integer) -> Integer
toggleSum x [] = 0
toggleSum x ((Left y) :: xs) = toggleSum y xs
toggleSum False ((Right y) :: xs) = toggleSum False xs
toggleSum True ((Right y) :: xs) = y + (toggleSum True xs)

export
sol2 : String -> String
sol2 = show . toggleSum True . part2