-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2_weird_error.pi
61 lines (57 loc) · 1.69 KB
/
part2_weird_error.pi
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
main =>
G = read_file_lines(),
H = G.len,
W = G[1].len,
Max = 0,
foreach (I in 1..W)
Max := max(Max, go([{I,1,south}])),
Max := max(Max, go([{I,H,north}])),
Max := max(Max, go([{1,I,east}])),
Max := max(Max, go([{W,I,west}])),
end,
println(Max).
go(Beams) = R =>
Energised = new_set(),
Seen = new_set(),
while (Beams.len > 0)
{X,Y,Dir} = Beams.head,
Beams := Beams.tail,
if not(Seen.has_key({X,Y,Dir})) then
Energised.put({X,Y}),
Seen.put({X,Y,Dir}),
New_beams = [{I,J,New_dir} : {I,J,New_dir} in step_beam(G[Y,X],X,Y,Dir), I >= 1, I <= W, J >= 1, J <=W],
foreach (Beam in New_beams)
Beams := [Beam|Beams]
end,
end,
end,
% foreach(J in 1..H)
% foreach(I in 1..W)
% if Energised.has_key({I,J}) then
% print("#"),
% else
% print(G[J,I]),
% end,
% end,
% nl,
% end,
R = Energised.size.
step_beam('.',X,Y,Dir) = [inc(X,Y,Dir)].
step_beam('|',X,Y,north) = [inc(X,Y,north)].
step_beam('|',X,Y,south) = [inc(X,Y,south)].
step_beam('|',X,Y,_) = [inc(X,Y,north),inc(X,Y,south)].
step_beam('-',X,Y,east) = [inc(X,Y,east)].
step_beam('-',X,Y,west) = [inc(X,Y,west)].
step_beam('-',X,Y,_) = [inc(X,Y,west),inc(X,Y,east)].
step_beam('/',X,Y,east) = [{X,Y-1,north}].
step_beam('/',X,Y,west) = [{X,Y+1,south}].
step_beam('/',X,Y,north) = [{X+1,Y,east}].
step_beam('/',X,Y,south) = [{X-1,Y,west}].
step_beam('\\',X,Y,east) = [inc(X,Y,south)].
step_beam('\\',X,Y,west) = [inc(X,Y,north)].
step_beam('\\',X,Y,north) = [inc(X,Y,west)].
step_beam('\\',X,Y,south) = [inc(X,Y,east)].
inc(X,Y,north) = {X,Y-1,north}.
inc(X,Y,east) = {X+1,Y,east}.
inc(X,Y,south) = {X,Y+1,south}.
inc(X,Y,west) = {X-1,Y,west}.