-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathpart1.dylan
73 lines (66 loc) · 1.92 KB
/
part1.dylan
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
module: part1
define constant $n = 251;
let m = make(<vector>, size: $n);
for (i from 0 below $n)
m[i] := make(<vector>, size: $n, fill: '#')
end;
for (i from 1 below $n - 1 by 2)
for (j from 1 below $n by 2)
m[i][j] := '.'
end
end;
let r = read-line(*standard-input*);
define function f(i, q)
let pq = q.shallow-copy;
let s = make(<set>);
local method move(q, i, j)
let t = make(<set>);
for (p in q)
let (y, x) = values(p.head, p.tail);
m[y + i][x + j] := ' ';
add!(t, pair(y + i * 2, x + j * 2))
end;
t
end;
local method loop(k)
if (k >= r.size - 1 | r[k] = ')')
values(k, s)
else
select (r[k])
'(' =>
let (nk, nq) = f(k + 1, q);
k := nk;
q := nq;
'|' =>
for (e in q) add!(s, e) end;
q := pq.shallow-copy;
'N' => q := move(q, -1, 0);
'S' => q := move(q, 1, 0);
'W' => q := move(q, 0, -1);
'E' => q := move(q, 0, 1);
end;
loop(k + 1)
end;
end;
loop(i)
end;
let p = make(<set>);
let (y, x) = values(floor/($n, 2), floor/($n, 2));
f(1, add!(p, pair(y, x)));
let v = make(<vector>, size: $n);
for (i from 0 below $n)
v[i] := make(<vector>, size: $n, fill: #f);
end;
let q = make(<deque>, size: 1, fill: vector(y, x, 0));
let c = #f;
until (q.empty?)
let e = pop(q);
let (y, x, d) = values(e[0], e[1], e[2]);
c := d;
v[y][x] := d;
when (m[y - 1][x] = ' ' & ~v[y - 2][x]) push-last(q, vector(y - 2, x, d + 1)) end;
when (m[y + 1][x] = ' ' & ~v[y + 2][x]) push-last(q, vector(y + 2, x, d + 1)) end;
when (m[y][x - 1] = ' ' & ~v[y][x - 2]) push-last(q, vector(y, x - 2, d + 1)) end;
when (m[y][x + 1] = ' ' & ~v[y][x + 2]) push-last(q, vector(y, x + 2, d + 1)) end
end;
format-out("%=\n", c);