-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay15.hs
More file actions
52 lines (45 loc) · 1.95 KB
/
Copy pathDay15.hs
File metadata and controls
52 lines (45 loc) · 1.95 KB
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 Day15
( part1
, part2
) where
import Data.Graph.Inductive.Graph (labNodes)
import Data.Graph.Inductive.Query.BFS (esp, level)
import Data.HashSet as St (HashSet, empty, insert,
member, singleton,
toList)
import Data.List as L (unfoldr)
import Data.Sequence as Sq (Seq ((:|>)),
ViewL (EmptyL, (:<)),
singleton, viewl)
import Helpers.Graph (Gr, Pos, assocsToDigraph, dirs,
origin, unfoldAssocs)
import Intcode (Intcode, initialise,
runIntcode, sendInput)
buildGraph :: Intcode -> Gr (Pos, Int) Int
buildGraph intcode =
assocsToDigraph . unfoldr (unfoldAssocs buildCons fst) $
(Sq.singleton ((origin, 1), intcode), St.singleton (origin, 1))
where
buildCons set pos =
filter (\((x, _), _) -> snd x /= 0 && not (x `member` set)) .
map (\(a, (b, c)) -> (((a, head b), c), 1)) .
zipWith
(\a b -> (fst (fst pos) + a, runIntcode . sendInput b . snd $ pos))
dirs $
[1, 2, 4, 3]
oxygenTank :: Gr (Pos, Int) Int -> Int
oxygenTank = fst . head . filter ((== 2) . snd . snd) . labNodes
findOxygenTank :: Intcode -> Int
findOxygenTank intcode = (-1 +) . length . esp 0 oxTank $ area
where
area = buildGraph intcode
oxTank = oxygenTank area
fillArea :: Intcode -> Int
fillArea intcode = maximum . map snd . level oxTank $ area
where
area = buildGraph intcode
oxTank = oxygenTank area
part1 :: Bool -> String -> String
part1 _ = show . findOxygenTank . initialise
part2 :: Bool -> String -> String
part2 _ = show . fillArea . initialise