-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC11-Maple.mpl
104 lines (94 loc) · 2.21 KB
/
AoC11-Maple.mpl
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
#sample
tinput := "5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526";
#toy
tinput := "11111
19991
19191
19991
11111";
input := FileTools:-Text:-ReadFile("AoC-2021-11-input.txt" ):
ogrid := map((parse~),StringTools:-Explode~(StringTools:-Split(input)));
gridw := nops(ogrid[1]); gridl := nops(ogrid);
neighbors := proc(pt) # include diagonals
local x, y, out;
global gridw, gridl;
out := NULL;
(x,y) := pt[];
if x > 1 then
out := out, [x-1, y];
if y > 1 then
out := out, [x-1, y-1];
end if;
end if;
if y > 1 then
out := out, [x, y-1];
if x< gridw then
out := out, [x+1, y-1];
end if;
end if;
if x < gridw then
out := out, [x+1, y];
if y < gridl then
out := out, [x+1, y+1];
end if;
end if;
if y < gridl then
out := out, [x, y+1];
if x > 1 then
out := out, [x-1, y+1];
end if;
end if;
return [out];
end proc:
flash := proc(x,y)
local n, nb := neighbors([x,y]);
global grid, flashed;
if flashed[x,y] <> 0 then
# flashed is a table to track that we flashed only once
return;
end if;
flashed[x,y] := 1;
for n in nb do
# increment neighbors and maybe trigger their flashes
grid[n[]] += 1;
if grid[n[]] > 9 then
flash(n[]);
end if;
end do;
end proc: # flash
totalflashes := 0;
grid := Array(ogrid);
for dd from 1 to 1000 while add(grid) <> 0 do
grid := grid +~ 1; # increment grid
flashed := table(sparse=0); # clear flash tracking table
# do the flashes - flash may trigger flashes earlier too
for i from 1 to gridw do
for j from 1 to gridl do
if grid[i,j] > 9 then
flash(i,j);
end if;
end do;
end do;
# count and reset flashed
for i from 1 to gridw do
for j from 1 to gridl do
if grid[i,j] > 9 then
grid[i,j] := 0;
if i <= 100 then
totalflashes+=1;
end if;
end if;
end do;
end do;
end do: # days
answer1 := totalflashes;
answer2 := (dd-1);