Day 08.
This commit is contained in:
parent
effae7bb4e
commit
a32b2c7628
|
@ -30,4 +30,4 @@ import qualified Day24
|
||||||
import qualified Day25
|
import qualified Day25
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = Day07.main
|
main = Day08.main
|
||||||
|
|
File diff suppressed because one or more lines are too long
23
src/Day08.hs
23
src/Day08.hs
|
@ -1,6 +1,25 @@
|
||||||
module Day08 (main) where
|
module Day08 (main) where
|
||||||
|
|
||||||
|
import Data.Tree (Tree(Node), rootLabel, subForest)
|
||||||
|
|
||||||
|
type Metadata = [Int]
|
||||||
|
|
||||||
|
parseTree :: ([Tree Metadata], [Int]) -> ([Tree Metadata], [Int])
|
||||||
|
parseTree (nodes, (c:m:input)) =
|
||||||
|
let (children, remaining) = iterate parseTree ([], input) !! c
|
||||||
|
in (Node (take m remaining) (reverse children) : nodes, drop m remaining)
|
||||||
|
|
||||||
|
part1 :: Tree Metadata -> Int
|
||||||
|
part1 = sum . fmap sum
|
||||||
|
|
||||||
|
part2 :: Tree Metadata -> Int
|
||||||
|
part2 tree =
|
||||||
|
if null (subForest tree) then sum (rootLabel tree)
|
||||||
|
else sum . map (part2 . (subForest tree !!) . subtract 1) . filter (<= length (subForest tree)) $ rootLabel tree
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "input/08.txt"
|
input <- map read . words <$> readFile "input/08.txt"
|
||||||
print input
|
let tree = head . fst $ parseTree ([], input)
|
||||||
|
print $ part1 tree
|
||||||
|
print $ part2 tree
|
Loading…
Reference in New Issue