Advent of Code solutions
1import Control.Monad
2import Text.Parsec
3
4int = read <$> many digit
5
6countdigits n
7 | n < 10 = 1
8 | otherwise = 1 + countdigits (n `div` 10)
9
10apply :: (Int -> Int -> Int) -> [Int] -> [Int]
11apply op (x : y : rest) = op x y : rest
12
13findAnswer ans [a]
14 | a == ans = [()]
15 | otherwise = []
16findAnswer ans terms = do
17 apd <- apply <$> [(+), (*)] <*> [terms]
18 findAnswer ans apd
19
20solvable = not . null . uncurry findAnswer
21
22equation = do
23 answer <- int
24 char ':'
25 space
26 terms <- int `sepBy` char ' '
27 return (answer, terms)
28
29compute = sum . fmap fst . filter solvable <$> equation `sepEndBy` newline
30
31answer input = ans
32 where
33 Right ans = parse compute "" input
34
35main = getContents >>= print . answer