Skip to content

Commit 4d0a669

Browse files
authored
Merge pull request #17 from romellem/day-17
2018 - Day 17
2 parents a0dcd15 + 31d8018 commit 4d0a669

File tree

16 files changed

+6317
-2
lines changed

16 files changed

+6317
-2
lines changed

2018/17/PLAN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```
2+
Let `drips` be a Set of Points, tracking:
3+
- x
4+
- y
5+
init'd with a drip at 500,0
6+
7+
Loop through drips until we have no more, with each one:
8+
9+
if cell below is greater than max,
10+
delete drip and bail
11+
12+
if cell below current drip is SAND,
13+
mark map below current as FLOWING
14+
move drip down 1
15+
if cell below current drip is CLAY or SETTLED,
16+
Move left, checking floor below continually to remain CLAY or SETTLED
17+
If we eventually meet CLAY or SETTLED to the left,
18+
var barrier_left = {x,y} of barrier
19+
else
20+
var flow_left = {x,y} of where the drip would start
21+
Move right, checking floor below continually to stay CLAY or SETTLED
22+
If we eventually meet CLAY or SETTLED to the right,
23+
var barrier_right = {x,y}
24+
else
25+
var flow_right = {x,y} of where the drip would start
26+
If barrier_left AND barrier_right
27+
Mark from barrier_left to barrier_right to be SETTLED
28+
move drip up 1
29+
else if barrier_left AND flow_right
30+
mark from barrier_left to flow_right to be FLOWING
31+
move drip to flow_right
32+
else if barrier_right AND flow_left
33+
mark from flow_left to barrier_right to be FLOWING
34+
move drip to flow_left
35+
else if flow_left AND flow_right
36+
mark from flow_left to flow_right to be FLOWING
37+
move drip to flow_left
38+
push new drip to flow_right
39+
40+
```

2018/17/README.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Answers
2+
3+
| Part 1 | Part 2 |
4+
|---------|---------|
5+
| `36790` | `30765` |
6+
7+
## --- Day 17: Reservoir Research ---
8+
9+
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.
10+
11+
Rather, it hasn't been constructed _yet_. 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?
12+
13+
You scan a two-dimensional vertical slice of the ground nearby and discover that it is mostly _sand_ with veins of _clay_. The scan only provides data with a granularity of _square meters_, 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 _spring of water_ near the surface at `x=500, y=0`. The scan identifies _which square meters are clay_ (your puzzle input).
14+
15+
For example, suppose your scan shows the following veins of clay:
16+
17+
x=495, y=2..7
18+
y=7, x=495..501
19+
x=501, y=3..7
20+
x=498, y=2..4
21+
x=506, y=1..2
22+
x=498, y=10..13
23+
x=504, y=10..13
24+
y=13, x=498..504
25+
26+
27+
Rendering clay as `#`, sand as `.`, and the water spring as `+`, and with `x` increasing to the right and `y` increasing downward, this becomes:
28+
29+
44444455555555
30+
99999900000000
31+
45678901234567
32+
0 ......+.......
33+
1 ............#.
34+
2 .#..#.......#.
35+
3 .#..#..#......
36+
4 .#..#..#......
37+
5 .#.....#......
38+
6 .#.....#......
39+
7 .#######......
40+
8 ..............
41+
9 ..............
42+
10 ....#.....#...
43+
11 ....#.....#...
44+
12 ....#.....#...
45+
13 ....#######...
46+
47+
48+
The spring of water will produce water _forever_. Water can move through sand, but is blocked by clay. Water _always moves down_ when possible, and spreads to the left and right otherwise, filling space that has clay on both sides and falling out otherwise.
49+
50+
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 `|`:
51+
52+
......+.......
53+
......|.....#.
54+
.#..#.|.....#.
55+
.#..#.|#......
56+
.#..#.|#......
57+
.#....|#......
58+
.#~~~~~#......
59+
.#######......
60+
..............
61+
..............
62+
....#.....#...
63+
....#.....#...
64+
....#.....#...
65+
....#######...
66+
67+
68+
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:
69+
70+
......+.......
71+
......|.....#.
72+
.#..#.|.....#.
73+
.#..#.|#......
74+
.#..#.|#......
75+
.#~~~~~#......
76+
.#~~~~~#......
77+
.#######......
78+
..............
79+
..............
80+
....#.....#...
81+
....#.....#...
82+
....#.....#...
83+
....#######...
84+
85+
86+
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:
87+
88+
......+.......
89+
......|.....#.
90+
.#..#.|.....#.
91+
.#..#~~#......
92+
.#..#~~#......
93+
.#~~~~~#......
94+
.#~~~~~#......
95+
.#######......
96+
..............
97+
..............
98+
....#.....#...
99+
....#.....#...
100+
....#.....#...
101+
....#######...
102+
103+
104+
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:
105+
106+
<pre><code>......+.......
107+
......|.....#.
108+
.#..#<b>|</b>|||...#.
109+
.#..#~~#|.....
110+
.#..#~~#|.....
111+
.#~~~~~#|.....
112+
.#~~~~~#|.....
113+
.#######|.....
114+
........|.....
115+
........|.....
116+
....#...|.#...
117+
....#...|.#...
118+
....#~~~~~#...
119+
....#######...
120+
</code></pre>
121+
122+
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.
123+
124+
After 10 more squares of water, the bottom reservoir is also full:
125+
126+
......+.......
127+
......|.....#.
128+
.#..#||||...#.
129+
.#..#~~#|.....
130+
.#..#~~#|.....
131+
.#~~~~~#|.....
132+
.#~~~~~#|.....
133+
.#######|.....
134+
........|.....
135+
........|.....
136+
....#~~~~~#...
137+
....#~~~~~#...
138+
....#~~~~~#...
139+
....#######...
140+
141+
142+
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:
143+
144+
......+....... (line not counted: above minimum y value)
145+
......|.....#.
146+
.#..#||||...#.
147+
.#..#~~#|.....
148+
.#..#~~#|.....
149+
.#~~~~~#|.....
150+
.#~~~~~#|.....
151+
.#######|.....
152+
........|.....
153+
...|||||||||..
154+
...|#~~~~~#|..
155+
...|#~~~~~#|..
156+
...|#~~~~~#|..
157+
...|#######|..
158+
...|.......|.. (line not counted: below maximum y value)
159+
...|.......|.. (line not counted: below maximum y value)
160+
...|.......|.. (line not counted: below maximum y value)
161+
162+
163+
How many tiles can be reached by the water? _To prevent counting forever_, 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.
164+
165+
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 _`57`_.
166+
167+
_How many tiles can the water reach_ within the range of `y` values in your scan?
168+
169+
-----------------
170+
171+
## --- Part Two ---
172+
173+
After a very long time, the water spring will run dry. How much water will be retained?
174+
175+
In the example above, water that won't eventually drain out is shown as `~`, a total of _`29`_ tiles.
176+
177+
_How many water tiles are left_ after the water spring stops producing water and all remaining water not at rest has drained?

2018/17/animation.mp4

451 KB
Binary file not shown.

2018/17/filled-grid.png

13.9 KB
Loading

0 commit comments

Comments
 (0)