Skip to content

Commit a495285

Browse files
committed
Solution for day 17.
1 parent 00c4544 commit a495285

File tree

6 files changed

+3536
-0
lines changed

6 files changed

+3536
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.exe
22
obj
3+
*.out

day17/day17.adb

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
with Ada.Text_IO; use Ada.Text_IO;
2+
with Input17; use Input17;
3+
4+
procedure Day17 is
5+
6+
Grid : array
7+
(Natural range 0 .. 2000, Natural range 0 .. 2000) of Character :=
8+
(others => (others => '.'));
9+
10+
Min_X, Min_Y : Natural := Natural'Last;
11+
Max_X, Max_Y : Natural := Natural'First;
12+
13+
function Can_Spread (X, Y : Natural) return Boolean is
14+
(Grid (X, Y) = '.' or Grid (X, Y) = '|');
15+
16+
procedure Spread_Water (X, Y : Natural) is
17+
begin
18+
if Y > Max_Y then
19+
return;
20+
end if;
21+
if not Can_Spread (X, Y) then
22+
return;
23+
end if;
24+
if not Can_Spread (X, Y + 1) then
25+
declare
26+
Left_X : Natural := X;
27+
Right_X : Natural := X + 1;
28+
begin
29+
while Can_Spread (Left_X, Y) and not Can_Spread (Left_X, Y + 1)
30+
loop
31+
Grid (Left_X, Y) := '|';
32+
Left_X := Left_X - 1;
33+
end loop;
34+
while Can_Spread (Right_X, Y) and not Can_Spread (Right_X, Y + 1)
35+
loop
36+
Grid (Right_X, Y) := '|';
37+
Right_X := Right_X + 1;
38+
end loop;
39+
if Can_Spread (Left_X, Y + 1) or Can_Spread (Right_X, Y + 1) then
40+
Spread_Water (Left_X, Y);
41+
Spread_Water (Right_X, Y);
42+
elsif Grid (Left_X, Y) = '#' and Grid (Right_X, Y) = '#' then
43+
for X2 in Left_X + 1 .. Right_X - 1 loop
44+
Grid (X2, Y) := '~';
45+
end loop;
46+
end if;
47+
end;
48+
elsif Grid (X, Y) = '.' then
49+
Grid (X, Y) := '|';
50+
Spread_Water (X, Y + 1);
51+
if Grid (X, Y + 1) = '~' then
52+
Spread_Water (X, Y);
53+
end if;
54+
end if;
55+
end Spread_Water;
56+
57+
begin
58+
for I in X_Inputs'Range loop
59+
Min_X := Natural'Min (Min_X, X_Inputs (I).Fixed);
60+
Max_X := Natural'Max (Max_X, X_Inputs (I).Fixed);
61+
Min_Y := Natural'Min (Min_Y, X_Inputs (I).First);
62+
Max_Y := Natural'Max (Max_Y, X_Inputs (I).Last);
63+
end loop;
64+
for I in Y_Inputs'Range loop
65+
Min_Y := Natural'Min (Min_Y, Y_Inputs (I).Fixed);
66+
Max_Y := Natural'Max (Max_Y, Y_Inputs (I).Fixed);
67+
Min_X := Natural'Min (Min_X, Y_Inputs (I).First);
68+
Max_X := Natural'Max (Max_X, Y_Inputs (I).Last);
69+
end loop;
70+
71+
for I in X_Inputs'Range loop
72+
declare
73+
X : constant Natural := X_Inputs (I).Fixed;
74+
begin
75+
for Y in X_Inputs (I).First .. X_Inputs (I).Last loop
76+
Grid (X, Y) := '#';
77+
end loop;
78+
end;
79+
end loop;
80+
for I in Y_Inputs'Range loop
81+
declare
82+
Y : constant Natural := Y_Inputs (I).Fixed;
83+
begin
84+
for X in Y_Inputs (I).First .. Y_Inputs (I).Last loop
85+
Grid (X, Y) := '#';
86+
end loop;
87+
end;
88+
end loop;
89+
90+
Spread_Water (500, Min_Y);
91+
92+
declare
93+
Water_Tiles : Natural := 0;
94+
Perma_Water : Natural := 0;
95+
begin
96+
for Y in Min_Y .. Max_Y loop
97+
for X in 0 .. 2000 loop
98+
if Grid (X, Y) = '~' then
99+
Perma_Water := Perma_Water + 1;
100+
Water_Tiles := Water_Tiles + 1;
101+
elsif Grid (X, Y) = '|' then
102+
Water_Tiles := Water_Tiles + 1;
103+
end if;
104+
end loop;
105+
end loop;
106+
Put_Line ("Part 1 =" & Natural'Image (Water_Tiles));
107+
Put_Line ("Part 2 =" & Natural'Image (Perma_Water));
108+
end;
109+
110+
-- Write the whole grid to a file (just for fun).
111+
declare
112+
File : File_Type;
113+
begin
114+
Create (File, Out_File, "grid.out");
115+
for Y in Min_Y .. Max_Y loop
116+
for X in Min_X .. Max_X loop
117+
Put (File, Grid (X, Y));
118+
end loop;
119+
Put_Line (File, "");
120+
end loop;
121+
Close (File);
122+
end;
123+
end Day17;

day17/day17.gpr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
project day17 is
2+
for Object_Dir use "obj";
3+
for Main use ("day17.adb");
4+
for Exec_Dir use ".";
5+
package Compiler is
6+
for Switches ("Ada") use ("-Wall", "-g", "-O2");
7+
end Compiler;
8+
end day17;

0 commit comments

Comments
 (0)