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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
--- Day 17: Reservoir Research ---
You arrive in the year 18. If it weren't for the coat you got in 1018, you would be very cold: the
North Pole base hasn't even been constructed.
Rather, it hasn't been constructed [1m[97myet[0m. The Elves are making a little progress, but there's not a
lot of liquid water in this climate, so they're getting very dehydrated. Maybe there's more
underground?
You scan a two-dimensional vertical slice of the ground nearby and discover that it is mostly
[1m[97msand[0m with veins of [1m[97mclay[0m. The scan only provides data with a granularity of [1m[97msquare meters[0m, but it
should be good enough to determine how much water is trapped there. In the scan, x represents the
distance to the right, and y represents the distance down. There is also a [1m[97mspring of water[0m near the
surface at x=500, y=0. The scan identifies [1m[97mwhich square meters are clay[0m (your puzzle input).
For example, suppose your scan shows the following veins of clay:
x=495, y=2..7
y=7, x=495..501
x=501, y=3..7
x=498, y=2..4
x=506, y=1..2
x=498, y=10..13
x=504, y=10..13
y=13, x=498..504
Rendering clay as #, sand as ., and the water spring as +, and with x increasing to the right and y
increasing downward, this becomes:
44444455555555
99999900000000
45678901234567
0 ......+.......
1 ............#.
2 .#..#.......#.
3 .#..#..#......
4 .#..#..#......
5 .#.....#......
6 .#.....#......
7 .#######......
8 ..............
9 ..............
10 ....#.....#...
11 ....#.....#...
12 ....#.....#...
13 ....#######...
The spring of water will produce water [1m[97mforever[0m. Water can move through sand, but is blocked by clay.
Water [1m[97malways moves down[0m when possible, and spreads to the left and right otherwise, filling space
that has clay on both sides and falling out otherwise.
For example, if five squares of water are created, they will flow downward until they reach the clay
and settle there. Water that has come to rest is shown here as ~, while sand through which water has
passed (but which is now dry again) is shown as |:
......+.......
......|.....#.
.#..#.|.....#.
.#..#.|#......
.#..#.|#......
.#....|#......
.#~~~~~#......
.#######......
..............
..............
....#.....#...
....#.....#...
....#.....#...
....#######...
Two squares of water can't occupy the same location. If another five squares of water are created,
they will settle on the first five, filling the clay reservoir a little more:
......+.......
......|.....#.
.#..#.|.....#.
.#..#.|#......
.#..#.|#......
.#~~~~~#......
.#~~~~~#......
.#######......
..............
..............
....#.....#...
....#.....#...
....#.....#...
....#######...
Water pressure does not apply in this scenario. If another four squares of water are created, they
will stay on the right side of the barrier, and no water will reach the left side:
......+.......
......|.....#.
.#..#.|.....#.
.#..#~~#......
.#..#~~#......
.#~~~~~#......
.#~~~~~#......
.#######......
..............
..............
....#.....#...
....#.....#...
....#.....#...
....#######...
At this point, the top reservoir overflows. While water can reach the tiles above the surface of the
water, it cannot settle there, and so the next five squares of water settle like this:
......+.......
......|.....#.
.#..#[1m[97m|[0m|||...#.
.#..#~~#|.....
.#..#~~#|.....
.#~~~~~#|.....
.#~~~~~#|.....
.#######|.....
........|.....
........|.....
....#...|.#...
....#...|.#...
....#~~~~~#...
....#######...
Note especially the leftmost |: the new squares of water can reach this tile, but cannot stop there.
Instead, eventually, they all fall to the right and settle in the reservoir below.
After 10 more squares of water, the bottom reservoir is also full:
......+.......
......|.....#.
.#..#||||...#.
.#..#~~#|.....
.#..#~~#|.....
.#~~~~~#|.....
.#~~~~~#|.....
.#######|.....
........|.....
........|.....
....#~~~~~#...
....#~~~~~#...
....#~~~~~#...
....#######...
Finally, while there is nowhere left for the water to settle, it can reach a few more tiles before
overflowing beyond the bottom of the scanned data:
......+....... (line not counted: above minimum y value)
......|.....#.
.#..#||||...#.
.#..#~~#|.....
.#..#~~#|.....
.#~~~~~#|.....
.#~~~~~#|.....
.#######|.....
........|.....
...|||||||||..
...|#~~~~~#|..
...|#~~~~~#|..
...|#~~~~~#|..
...|#######|..
...|.......|.. (line not counted: below maximum y value)
...|.......|.. (line not counted: below maximum y value)
...|.......|.. (line not counted: below maximum y value)
How many tiles can be reached by the water? [1m[97mTo prevent counting forever[0m, ignore tiles with a y
coordinate smaller than the smallest y coordinate in your scan data or larger than the largest one.
Any x coordinate is valid. In this example, the lowest y coordinate given is 1, and the highest is
13, causing the water spring (in row 0) and the water falling off the bottom of the render (in rows
14 through infinity) to be ignored.
So, in the example above, counting both water at rest (~) and other sand tiles the water can
hypothetically reach (|), the total number of tiles the water can reach is [1m[97m57[0m.
[1m[97mHow many tiles can the water reach[0m within the range of y values in your scan?
|