-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day8.hs
48 lines (40 loc) · 1.11 KB
/
Day8.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
module Javran.AdventOfCode.Y2018.Day8 (
) where
import Control.Monad
import Control.Monad.State.Strict
import Data.Functor.Base (TreeF (NodeF))
import Data.Functor.Foldable
import Data.Monoid
import Data.Tree
import Javran.AdventOfCode.Prelude
data Day8 deriving (Generic)
type N = Tree [Int]
parseNode :: State [Int] N
parseNode = do
~[n, m] <- state (splitAt 2)
subForest <- replicateM n parseNode
rootLabel <- state (splitAt m)
pure Node {rootLabel, subForest}
metaSum :: N -> Int
metaSum = cata \case
NodeF meta rs -> sum meta + sum rs
nodeValue :: N -> Int
nodeValue = cata \case
NodeF meta rs -> case rs of
[] -> sum meta
_ : _ ->
let sz = length rs
in getSum $
foldMap
( \i ->
if i > 0 && i <= sz
then Sum $ rs !! (i - 1)
else 0
)
meta
instance Solution Day8 where
solutionRun _ SolutionContext {getInputS, answerShow} = do
xs <- fmap (read @Int) . words . head . lines <$> getInputS
let n = evalState parseNode xs
answerShow (metaSum n)
answerShow (nodeValue n)