-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.hs
54 lines (44 loc) · 1.35 KB
/
Day17.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
53
54
module Day17
( part1
, part2
) where
import Data.Array.Unboxed (UArray, bounds, inRange, indices, (!))
import Helpers.Graph (Pos, dirs)
import Helpers.Parsers (make2DArray)
import Intcode (Intcode, clearOutput, execIntcode,
initialise, outputASCIIcode, outputIntcode,
sendASCIIInput, setMemory)
import Linear.V2 (V2 (..))
type Scaffold = UArray Pos Char
a = "R,4,L,10,L,10\n"
b = "L,8,R,12,R,10,R,4\n"
c = "L,8,L,8,R,10,R,4\n"
routine = "A,B,A,B,A,C,B,C,A,C\n"
align :: Scaffold -> Int
align scaffold =
sum .
map (\(V2 x y) -> x * y) .
filter
(\p ->
scaffold ! p == '#' &&
all (\d -> inRange b (p + d) && scaffold ! (p + d) == '#') dirs) .
indices $
scaffold
where
b = bounds scaffold
addInstruction :: String -> Intcode -> Intcode
addInstruction instruction = sendASCIIInput instruction . execIntcode
part1 :: Bool -> String -> String
part1 test
| test = show . align . make2DArray . lines
| otherwise =
show .
align .
make2DArray . filter (not . null) . lines . outputASCIIcode . initialise
part2 :: Bool -> String -> String
part2 _ =
show .
head .
outputIntcode .
flip (foldr addInstruction) ["n\n", c, b, a, routine] .
setMemory 0 2 . initialise