-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsol_11.aes
106 lines (96 loc) · 4.81 KB
/
sol_11.aes
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
@compiler >= 4.2.0
include "List.aes"
include "Pair.aes"
include "/Users/hans/Personal/Repos/AoC2019/11/Intcode.aes"
contract Day11 =
record pos = {dir : dir, x : int, y : int}
datatype dir = Up | Right | Down | Left
entrypoint solve_1() =
let p0 = Intcode.init_proc(code(), [0])
let grid = run_robot({dir = Up, x = 0, y = 0}, p0, {})
Map.size(grid)
entrypoint solve_2() =
let p0 = Intcode.init_proc(code(), [1])
let grid = run_robot({dir = Up, x = 0, y = 0}, p0, {})
output_grid(grid)
function run_robot(pos, p, grid) =
let p' = Intcode.run_proc_step(p)
switch(p'.output)
[dir, color] => // output is in reverse order
let grid' = grid{[(pos.x, pos.y)] = color}
if(p'.status == Intcode.Done) grid'
else
let pos' = move(pos, dir)
let input' = [grid'[(pos'.x, pos'.y) = 0]]
run_robot(pos', p'{output = [], input = input'}, grid')
function move(pos, d) =
if(d == 0) step(rotl(pos))
else step(rotr(pos))
function rotl(pos) =
switch(pos.dir)
Up => pos{dir = Left}
Right => pos{dir = Up}
Down => pos{dir = Right}
Left => pos{dir = Down}
function rotr(pos) =
switch(pos.dir)
Up => pos{dir = Right}
Right => pos{dir = Down}
Down => pos{dir = Left}
Left => pos{dir = Up}
function step(pos) =
switch(pos.dir)
Up => pos{y = pos.y + 1}
Right => pos{x = pos.x + 1}
Down => pos{y = pos.y - 1}
Left => pos{x = pos.x - 1}
function output_grid(m : map(int * int, int)) =
let cmp(a, b) = a =< b
let xs = List.sort(cmp, [p | ((p, _), _) <- Map.to_list(m)])
let ys = List.sort(cmp, [p | ((_, p), _) <- Map.to_list(m)])
let Some(xmin) = List.first(xs)
let Some(xmax) = List.last(xs)
let Some(ymin) = List.first(ys)
let Some(ymax) = List.last(ys)
let xs = [xmin..xmax]
List.reverse(List.map((y) => output_line(xs, y, m), [ymin..ymax]))
function output_line(xs, y, m) =
let to_str(c) = if(c == 1) "#" else " "
List.foldl((s, x) => String.concat(s, to_str(m[(x, y) = 2])), "", xs)
function code() =
[3, 8, 1005, 8, 291, 1106, 0, 11, 0, 0, 0, 104, 1, 104, 0, 3, 8, 1002, 8,
-1, 10, 101, 1, 10, 10, 4, 10, 108, 0, 8, 10, 4, 10, 1002, 8, 1, 28, 1,
1003, 20, 10, 2, 1103, 19, 10, 3, 8, 1002, 8, -1, 10, 1001, 10, 1, 10, 4,
10, 1008, 8, 0, 10, 4, 10, 1001, 8, 0, 59, 1, 1004, 3, 10, 3, 8, 102, -1,
8, 10, 1001, 10, 1, 10, 4, 10, 108, 0, 8, 10, 4, 10, 1001, 8, 0, 84, 1006,
0, 3, 1, 1102, 12, 10, 3, 8, 1002, 8, -1, 10, 101, 1, 10, 10, 4, 10, 1008,
8, 1, 10, 4, 10, 101, 0, 8, 114, 3, 8, 1002, 8, -1, 10, 101, 1, 10, 10, 4,
10, 108, 1, 8, 10, 4, 10, 101, 0, 8, 135, 3, 8, 1002, 8, -1, 10, 1001, 10,
1, 10, 4, 10, 1008, 8, 0, 10, 4, 10, 102, 1, 8, 158, 2, 9, 9, 10, 2, 2,
10, 10, 3, 8, 1002, 8, -1, 10, 1001, 10, 1, 10, 4, 10, 1008, 8, 1, 10, 4,
10, 101, 0, 8, 188, 1006, 0, 56, 3, 8, 1002, 8, -1, 10, 1001, 10, 1, 10,
4, 10, 108, 1, 8, 10, 4, 10, 1001, 8, 0, 212, 1006, 0, 76, 2, 1005, 8, 10,
3, 8, 102, -1, 8, 10, 1001, 10, 1, 10, 4, 10, 108, 1, 8, 10, 4, 10, 1001,
8, 0, 241, 3, 8, 102, -1, 8, 10, 101, 1, 10, 10, 4, 10, 1008, 8, 0, 10, 4,
10, 1002, 8, 1, 264, 1006, 0, 95, 1, 1001, 12, 10, 101, 1, 9, 9, 1007, 9,
933, 10, 1005, 10, 15, 99, 109, 613, 104, 0, 104, 1, 21102, 838484206484,
1, 1, 21102, 1, 308, 0, 1106, 0, 412, 21102, 1, 937267929116, 1, 21101, 0,
319, 0, 1105, 1, 412, 3, 10, 104, 0, 104, 1, 3, 10, 104, 0, 104, 0, 3, 10,
104, 0, 104, 1, 3, 10, 104, 0, 104, 1, 3, 10, 104, 0, 104, 0, 3, 10, 104,
0, 104, 1, 21102, 206312598619, 1, 1, 21102, 366, 1, 0, 1105, 1, 412,
21101, 179410332867, 0, 1, 21102, 377, 1, 0, 1105, 1, 412, 3, 10, 104, 0,
104, 0, 3, 10, 104, 0, 104, 0, 21101, 0, 709580595968, 1, 21102, 1, 400,
0, 1106, 0, 412, 21102, 868389384552, 1, 1, 21101, 411, 0, 0, 1106, 0,
412, 99, 109, 2, 21202, -1, 1, 1, 21102, 1, 40, 2, 21102, 1, 443, 3,
21101, 0, 433, 0, 1106, 0, 476, 109, -2, 2105, 1, 0, 0, 1, 0, 0, 1, 109,
2, 3, 10, 204, -1, 1001, 438, 439, 454, 4, 0, 1001, 438, 1, 438, 108, 4,
438, 10, 1006, 10, 470, 1102, 0, 1, 438, 109, -2, 2106, 0, 0, 0, 109, 4,
1202, -1, 1, 475, 1207, -3, 0, 10, 1006, 10, 493, 21102, 0, 1, -3, 21202,
-3, 1, 1, 21201, -2, 0, 2, 21101, 0, 1, 3, 21102, 1, 512, 0, 1106, 0, 517,
109, -4, 2105, 1, 0, 109, 5, 1207, -3, 1, 10, 1006, 10, 540, 2207, -4, -2,
10, 1006, 10, 540, 22101, 0, -4, -4, 1106, 0, 608, 21201, -4, 0, 1, 21201,
-3, -1, 2, 21202, -2, 2, 3, 21101, 0, 559, 0, 1106, 0, 517, 21201, 1, 0,
-4, 21102, 1, 1, -1, 2207, -4, -2, 10, 1006, 10, 578, 21101, 0, 0, -1,
22202, -2, -1, -2, 2107, 0, -3, 10, 1006, 10, 600, 21201, -1, 0, 1, 21102,
600, 1, 0, 106, 0, 475, 21202, -2, -1, -2, 22201, -4, -2, -4, 109, -5,
2106, 0, 0]