-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day09.hs
52 lines (45 loc) · 1.27 KB
/
Day09.hs
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
41
42
43
44
45
46
47
48
49
50
51
52
-- |
-- Module : AOC.Challenge.Day09
-- License : BSD3
--
-- Stability : experimental
-- Portability : non-portable
--
-- Day 9. See "AOC.Solver" for the types used in this module!
module AOC.Challenge.Day09 (
day09a
, day09b
) where
import AOC.Solver ((:~>)(..))
import Data.Char (isSpace)
chompWith :: (Int -> Int -> String -> Int) -> String -> Int
chompWith f = coutside 0
where
coutside n str = case span (/= '(') str of
(o,[]) -> n + length o
(o,_:is) -> cinside (n + length o) is
cinside n str = case span (/= ')') str of
(_,[]) -> error "what"
(i,_:os) -> case span (/= 'x') i of
(a,_:b) ->
let a' = read a
b' = read b
(taken,os') = splitAt (read a) os
in coutside (n + f a' b' taken) os'
(_,[] ) -> error "what"
chompOnce :: String -> Int
chompOnce = chompWith $ \x y _ -> x * y
day09a :: String :~> Int
day09a = MkSol
{ sParse = Just . filter (not . isSpace)
, sShow = show
, sSolve = Just . chompOnce
}
chompRec :: String -> Int
chompRec = chompWith $ \_ y str -> chompRec str * y
day09b :: String :~> Int
day09b = MkSol
{ sParse = Just . filter (not . isSpace)
, sShow = show
, sSolve = Just . chompRec
}