-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.hs
executable file
·32 lines (25 loc) · 934 Bytes
/
part1.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
#!/usr/bin/env runghc
module Day7.Part1 where
import Day7
import Data.Array
import Data.Graph
import Data.Maybe
main = interact (show . solve "shiny gold" . parse rules)
-- solve myBag rs = (+) (-1) $ forestSize $ dfs graph [vertex]
-- solve myBag rs = length $ keys -- filter (path graph vertex . fromJust . vertexFromKey) keys
solve myBag rs = graph
where keys = filter (/= myBag) $ map ruleToKey rs
(graph, keyFromVertex, vertexFromKey) = graphFromEdges . map ruleToEdge $ rs
Just vertex = vertexFromKey myBag
ruleToKey = name . container
forestSize [] = 0
forestSize ((Node _ branches):siblings) = nodeSum + siblingSum + branchSum
where
nodeSum = 1
siblingSum = forestSize siblings
branchSum = forestSize branches
ruleToEdge :: Rule -> (String, String, [String])
ruleToEdge r = (node, node, edges)
where
node = name . container $ r
edges = map (name . item) . contents $ r