Reservoir Research


Fork me on GitHub
2018-12-17

Day 17: Reservoir Research

Description:
--- 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 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?

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).

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 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.

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:

......+.......
......|.....#.
.#..#||||...#.
.#..#~~#|.....
.#..#~~#|.....
.#~~~~~#|.....
.#~~~~~#|.....
.#######|.....
........|.....
........|.....
....#...|.#...
....#...|.#...
....#~~~~~#...
....#######...

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? 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.

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.

How many tiles can the water reach within the range of y values in your scan?

--- Part Two ---

After a very long time, the water spring will run dry. How much water will be retained?

In the example above, water that won't eventually drain out is shown as ~, a total of 29 tiles.

How many water tiles are left after the water spring stops producing water and all remaining water not at rest has drained?

Input:
x=573, y=917..931
x=574, y=1553..1568
x=544, y=1152..1162
x=567, y=1883..1895
x=535, y=230..233
x=566, y=360..373
y=1537, x=512..531
x=556, y=675..687
y=41, x=499..515
x=496, y=1586..1594
x=483, y=237..250
y=69, x=498..507
y=1912, x=560..569
y=756, x=493..495
x=487, y=1548..1556
y=1036, x=522..524
y=21, x=493..507
y=1096, x=530..532
x=506, y=1669..1674
x=537, y=919..936
x=563, y=1772..1777
y=1260, x=486..488
x=578, y=1538..1540
y=1159, x=551..555
y=1015, x=533..549
x=486, y=1424..1430
x=520, y=183..199
y=167, x=537..543
x=531, y=15..25
y=1333, x=486..492
x=570, y=398..408
x=539, y=1282..1293
x=569, y=721..723
y=1719, x=494..497
x=575, y=865..875
y=973, x=528..548
y=576, x=558..561
y=164, x=512..515
y=1567, x=508..519
x=508, y=1670..1674
x=523, y=296..310
x=517, y=1793..1797
x=493, y=653..666
x=542, y=1925..1937
y=355, x=568..591
x=556, y=1776..1783
y=924, x=497..513
x=552, y=180..197
x=520, y=1430..1440
y=167, x=507..527
y=1331, x=518..520
x=558, y=1398..1416
y=1674, x=506..508
x=565, y=1140..1156
y=325, x=529..531
y=529, x=491..494
x=560, y=197..207
x=513, y=1371..1389
x=547, y=1068..1078
x=508, y=826..829
x=565, y=843..854
x=570, y=773..785
y=1039, x=515..533
x=543, y=93..104
y=301, x=534..559
x=507, y=319..331
x=564, y=899..912
y=1252, x=522..539
x=576, y=1260..1262
y=1654, x=545..548
x=533, y=456..470
y=1384, x=505..507
x=528, y=963..973
x=531, y=1670..1674
x=512, y=1527..1537
y=1187, x=522..551
y=1078, x=547..549
y=633, x=512..514
x=524, y=1202..1216
x=516, y=1491..1498
x=553, y=162..172
y=408, x=505..570
x=530, y=1624..1632
y=687, x=556..576
x=503, y=1599..1604
x=564, y=322..325
x=525, y=1582..1594
y=1556, x=487..502
x=507, y=68..69
y=1632, x=524..530
x=511, y=400..405
y=697, x=485..500
x=559, y=1573..1595
x=588, y=659..685
y=1568, x=532..538
y=895, x=497..499
x=579, y=1760..1764
y=760, x=520..524
y=101, x=551..554
y=224, x=552..557
x=520, y=1321..1331
x=510, y=1408..1419
x=522, y=911..930
y=625, x=501..503
x=566, y=862..872
y=1361, x=559..562
y=579, x=552..570
x=507, y=157..167
x=484, y=495..504
y=854, x=563..565
y=763, x=506..513
y=1535, x=535..557
x=580, y=775..781
x=566, y=45..65
x=544, y=1469..1482
x=568, y=353..355
y=1316, x=509..514
x=581, y=1065..1078
y=1782, x=524..526
x=567, y=969..971
y=1636, x=518..536
x=491, y=1664..1667
y=1019, x=567..577
x=559, y=1346..1361
x=579, y=566..574
x=562, y=417..430
y=1228, x=584..591
x=574, y=1149..1153
x=527, y=227..237
x=486, y=390..405
y=1437, x=527..529
x=544, y=1390..1394
y=207, x=560..581
x=541, y=1540..1551
x=497, y=1600..1604
y=936, x=532..537
y=343, x=543..547
x=549, y=154..158
x=524, y=1667..1677
y=1760, x=496..503
x=529, y=322..325
x=543, y=70..82
x=581, y=1760..1764
x=497, y=1451..1456
x=493, y=746..756
x=533, y=1375..1377
x=497, y=821..833
x=544, y=827..847
y=1143, x=521..544
x=541, y=454..467
y=197, x=544..552
x=548, y=1674..1681
y=1233, x=530..546
x=549, y=899..908
x=498, y=596..610
x=514, y=597..610
x=588, y=446..469
x=557, y=206..224
y=1705, x=564..572
x=498, y=296..310
x=541, y=1706..1715
x=531, y=259..271
x=564, y=1044..1052
x=494, y=520..529
y=1715, x=538..541
x=485, y=695..697
x=542, y=380..384
x=557, y=864..875
y=1368, x=502..505
x=521, y=1131..1143
y=912, x=564..589
x=551, y=1174..1187
y=1359, x=526..546
y=1614, x=520..524
x=498, y=1081..1084
x=499, y=873..895
y=328, x=515..538
x=509, y=1113..1125
y=104, x=543..563
x=583, y=775..781
x=540, y=486..490
x=566, y=77..102
x=553, y=479..489
x=507, y=1583..1594
y=322, x=564..570
x=562, y=653..663
x=518, y=1627..1636
x=582, y=1618..1631
x=581, y=736..748
y=1052, x=564..581
x=498, y=90..103
y=706, x=554..572
x=563, y=692..703
x=542, y=29..54
y=1870, x=502..504
y=1852, x=550..571
x=577, y=634..652
x=573, y=1439..1444
y=164, x=561..584
x=564, y=1679..1705
x=568, y=116..142
y=99, x=506..509
x=555, y=155..158
x=521, y=52..55
x=483, y=1569..1571
x=508, y=414..419
y=65, x=557..566
y=1125, x=509..511
y=489, x=553..570
x=562, y=477..486
x=486, y=1309..1333
x=567, y=1016..1019
x=550, y=827..847
y=1742, x=543..560
x=535, y=1050..1059
y=1591, x=512..519
x=532, y=1565..1568
y=1718, x=533..549
x=541, y=521..531
x=541, y=713..725
x=507, y=10..21
y=765, x=554..578
x=548, y=962..973
x=582, y=1418..1423
y=1848, x=556..559
y=1742, x=510..528
x=549, y=1751..1762
x=543, y=334..343
x=544, y=1131..1143
x=489, y=361..374
x=559, y=113..127
y=436, x=538..546
x=589, y=888..889
x=560, y=1485..1494
y=1414, x=518..521
x=535, y=182..199
y=467, x=541..543
x=533, y=1003..1015
x=565, y=735..745
x=534, y=139..141
x=550, y=634..643
y=102, x=566..585
x=567, y=1096..1116
x=551, y=794..806
x=529, y=1375..1377
x=580, y=1448..1472
x=519, y=1203..1216
x=510, y=1734..1742
x=510, y=1816..1834
x=485, y=1169..1173
x=559, y=278..301
y=785, x=505..507
x=585, y=1538..1540
x=513, y=884..893
x=522, y=256..264
y=267, x=492..504
y=663, x=539..562
x=544, y=587..600
x=525, y=1093..1102
y=1477, x=484..488
y=944, x=495..501
x=526, y=984..987
y=1498, x=510..516
x=513, y=361..374
x=589, y=1333..1343
x=561, y=1925..1937
x=498, y=67..69
x=548, y=1611..1627
y=1456, x=487..497
x=506, y=654..666
x=527, y=1435..1437
x=512, y=1765..1771
x=545, y=1643..1654
x=533, y=1708..1718
x=532, y=920..936
x=512, y=1713..1726
x=561, y=1820..1833
x=512, y=154..164
y=1102, x=500..508
y=705, x=542..544
x=524, y=202..212
x=507, y=1378..1384
x=530, y=1451..1464
y=430, x=562..585
x=543, y=155..167
y=127, x=541..559
y=1808, x=531..551
y=1440, x=520..535
x=567, y=1292..1302
x=580, y=1168..1170
x=558, y=1777..1783
y=1674, x=531..536
y=52, x=521..524
x=522, y=1240..1252
y=172, x=551..553
x=562, y=880..893
x=511, y=519..521
x=577, y=1016..1019
x=580, y=1862..1872
y=1872, x=561..580
x=553, y=1387..1397
x=571, y=1448..1472
x=515, y=154..164
y=21, x=569..585
x=551, y=496..509
y=500, x=525..533
y=721, x=546..548
x=544, y=1562..1573
x=518, y=1321..1331
y=1653, x=512..526
y=785, x=570..589
x=488, y=1475..1477
x=509, y=137..146
y=1548, x=573..591
x=495, y=937..944
x=584, y=150..164
x=498, y=389..405
y=1288, x=531..533
x=544, y=688..705
y=1845, x=486..488
x=566, y=1438..1444
x=575, y=1233..1243
x=551, y=1795..1808
y=1262, x=574..576
x=564, y=477..486
x=556, y=846..859
x=555, y=1070..1082
x=551, y=1024..1042
y=346, x=538..557
x=578, y=9..14
x=500, y=1150..1162
y=1888, x=523..534
x=551, y=1149..1159
x=538, y=1390..1394
x=552, y=1907..1917
x=568, y=543..554
y=1530, x=521..525
y=1460, x=545..559
y=269, x=548..573
x=508, y=1095..1102
x=559, y=1844..1848
x=526, y=1645..1653
x=530, y=279..289
x=552, y=899..908
y=405, x=511..547
x=497, y=177..191
x=582, y=1650..1673
x=487, y=570..580
y=271, x=514..531
y=1604, x=497..503
y=285, x=521..523
y=1785, x=518..537
x=566, y=176..178
x=522, y=1047..1054
x=580, y=1838..1849
y=1595, x=549..559
x=534, y=279..301
x=563, y=608..622
x=497, y=874..895
x=560, y=1909..1912
x=490, y=1227..1229
x=500, y=495..504
x=591, y=447..469
y=521, x=508..511
x=532, y=1096..1098
y=703, x=563..565
y=666, x=493..506
x=533, y=1026..1039
x=544, y=28..54
x=528, y=1910..1922
y=509, x=551..569
y=930, x=520..522
x=526, y=1346..1359
x=565, y=1346..1374
y=191, x=497..515
x=512, y=1072..1094
x=498, y=1168..1173
x=552, y=1304..1314
y=748, x=578..581
x=506, y=453..459
x=550, y=1840..1852
y=1777, x=563..572
x=556, y=964..975
x=530, y=943..955
y=1042, x=543..551
x=558, y=902..911
x=552, y=543..554
x=580, y=580..585
x=585, y=78..102
y=889, x=579..589
x=522, y=1506..1517
y=580, x=487..502
y=1820, x=579..587
y=1271, x=539..558
x=533, y=984..987
y=1284, x=512..516
x=504, y=1866..1870
x=570, y=322..325
x=591, y=1525..1548
y=1162, x=544..562
x=557, y=307..309
x=589, y=37..57
x=538, y=1343..1356
y=829, x=505..508
y=237, x=527..545
x=569, y=497..509
y=373, x=566..581
x=569, y=1909..1912
x=544, y=612..624
y=1763, x=556..570
y=1865, x=513..521
y=299, x=515..517
x=505, y=1157..1159
x=537, y=156..167
x=530, y=613..624
y=1094, x=512..517
y=987, x=526..533
x=561, y=150..164
y=469, x=588..591
x=495, y=1764..1771
y=771, x=536..549
y=685, x=584..588
x=483, y=189..206
y=1026, x=559..584
y=478, x=496..516
x=513, y=1848..1865
x=501, y=423..447
x=560, y=1552..1568
x=540, y=1686..1696
x=560, y=1729..1742
y=1288, x=483..485
x=539, y=652..663
x=546, y=1411..1414
y=1352, x=573..577
y=1380, x=518..539
x=515, y=1227..1229
y=1078, x=558..581
x=483, y=1279..1288
y=911, x=540..558
y=1216, x=571..576
x=523, y=674..681
x=563, y=714..725
y=1170, x=556..580
x=558, y=1064..1078
x=512, y=1816..1834
x=483, y=13..32
x=515, y=30..41
x=591, y=1837..1849
x=551, y=91..101
y=823, x=558..582
x=513, y=913..924
y=1617, x=515..534
x=548, y=1364..1374
x=514, y=258..271
y=195, x=527..529
x=512, y=665..685
x=570, y=1510..1512
x=551, y=669..680
x=538, y=960..968
y=325, x=499..501
x=592, y=1883..1895
x=500, y=286..292
x=575, y=606..616
y=1937, x=542..561
x=538, y=1507..1517
y=1501, x=499..522
y=1631, x=574..582
x=576, y=1201..1216
x=551, y=111..122
y=103, x=498..517
x=515, y=846..867
x=528, y=339..349
x=502, y=1910..1922
x=577, y=1352..1360
x=538, y=336..346
y=1084, x=494..498
x=493, y=1839..1862
x=547, y=485..490
x=536, y=760..771
x=559, y=1007..1026
x=538, y=1706..1715
y=57, x=575..589
y=32, x=483..488
y=1302, x=567..576
x=502, y=1361..1368
x=524, y=1469..1482
y=971, x=567..571
x=585, y=6..21
x=517, y=377..389
y=419, x=483..508
x=506, y=743..763
y=908, x=549..552
y=1268, x=545..547
x=520, y=1611..1614
x=545, y=1246..1253
x=573, y=606..616
y=1932, x=547..554
x=565, y=1486..1494
x=515, y=318..328
y=1430, x=486..501
x=492, y=1309..1333
y=585, x=580..585
x=585, y=579..585
x=576, y=818..820
x=571, y=1202..1216
y=742, x=555..559
x=547, y=334..343
x=515, y=1025..1039
x=556, y=494..505
x=562, y=1029..1034
y=329, x=557..580
x=492, y=1793..1796
x=543, y=1540..1551
y=624, x=530..544
x=538, y=426..436
y=310, x=498..523
x=520, y=743..760
x=576, y=1906..1917
x=553, y=111..122
x=506, y=88..99
x=551, y=1542..1555
x=555, y=377..389
y=505, x=556..561
x=527, y=156..167
x=505, y=397..408
x=584, y=659..685
y=813, x=502..527
x=516, y=1270..1284
y=55, x=521..524
x=494, y=1081..1084
y=504, x=484..500
x=531, y=1528..1537
x=574, y=1261..1262
x=558, y=520..531
x=532, y=1504..1514
x=538, y=318..328
x=508, y=984..993
y=448, x=553..575
y=680, x=546..551
x=504, y=1027..1032
x=561, y=1862..1872
y=1243, x=563..575
x=503, y=620..625
x=591, y=1215..1228
x=541, y=960..968
x=572, y=1772..1777
y=1673, x=582..590
y=1771, x=495..512
x=555, y=733..742
x=534, y=1109..1123
x=490, y=1815..1821
x=583, y=117..142
x=503, y=1886..1896
x=588, y=942..955
x=523, y=1870..1888
x=529, y=665..685
x=522, y=1173..1187
x=527, y=139..141
y=1159, x=505..520
y=1009, x=485..493
y=867, x=506..515
y=1658, x=536..554
x=577, y=236..252
y=1762, x=528..549
y=1356, x=536..538
x=576, y=674..687
y=1173, x=485..498
x=547, y=400..405
y=141, x=527..534
x=552, y=565..579
y=230, x=504..523
x=548, y=1643..1654
x=554, y=91..101
y=349, x=504..528
x=564, y=1397..1416
x=543, y=1730..1742
y=1360, x=573..577
y=1149, x=570..574
y=725, x=541..563
x=515, y=299..305
x=487, y=1452..1456
x=485, y=48..62
x=501, y=621..625
x=583, y=722..723
y=1793, x=510..517
y=206, x=483..492
x=546, y=1055..1061
x=524, y=453..459
y=1070, x=566..572
y=380, x=532..542
x=542, y=880..893
y=1844, x=503..506
x=569, y=6..21
x=510, y=427..433
x=539, y=1819..1833
x=533, y=1151..1162
x=505, y=1378..1384
y=1153, x=570..574
x=521, y=629..647
x=509, y=1298..1316
x=502, y=570..580
y=433, x=510..512
x=509, y=88..99
x=516, y=118..122
x=527, y=788..813
y=984, x=526..533
x=523, y=276..285
x=514, y=1299..1316
x=512, y=1270..1284
y=1517, x=522..538
x=584, y=1008..1026
x=539, y=1369..1380
y=616, x=573..575
x=526, y=1305..1314
y=1374, x=565..586
y=1116, x=567..590
y=588, x=512..527
x=551, y=136..146
y=1555, x=533..551
x=499, y=321..325
x=557, y=314..329
x=512, y=1644..1653
x=516, y=1049..1059
x=543, y=454..467
x=527, y=1504..1514
y=459, x=506..524
y=1551, x=541..543
x=529, y=192..195
x=538, y=1565..1568
y=999, x=557..566
x=581, y=1044..1052
x=591, y=352..355
x=561, y=236..252
x=517, y=91..103
x=503, y=1743..1760
x=546, y=717..721
x=546, y=736..745
x=499, y=717..721
x=573, y=1525..1548
y=643, x=550..569
y=1211, x=540..563
x=578, y=1418..1423
y=681, x=521..523
x=518, y=1368..1380
x=501, y=1424..1430
y=639, x=512..514
y=1389, x=495..513
y=1229, x=490..515
x=575, y=947..950
x=537, y=230..233
y=1917, x=552..576
y=1696, x=536..540
x=514, y=822..833
x=588, y=846..858
x=578, y=737..748
x=494, y=118..122
x=514, y=633..639
y=1394, x=538..544
x=507, y=285..292
y=1673, x=483..498
x=525, y=1530..1534
x=565, y=19..23
x=563, y=843..854
x=575, y=831..840
x=539, y=982..992
x=491, y=520..529
x=543, y=1023..1042
y=1423, x=578..582
y=1156, x=565..580
y=1642, x=493..505
x=519, y=1551..1567
x=524, y=1777..1782
y=893, x=542..562
x=589, y=772..785
x=534, y=884..893
x=552, y=1612..1627
y=1397, x=527..553
x=508, y=520..521
x=512, y=278..289
x=573, y=818..820
x=556, y=1750..1763
x=576, y=1292..1302
x=578, y=755..765
y=975, x=556..581
x=505, y=985..993
x=552, y=205..224
x=547, y=1257..1268
x=506, y=847..867
x=567, y=920..927
y=931, x=552..573
y=199, x=520..535
x=492, y=190..206
x=556, y=792..803
x=546, y=1345..1359
x=484, y=318..331
y=1087, x=488..506
y=470, x=533..553
x=510, y=217..226
x=517, y=299..305
x=526, y=1323..1334
y=1472, x=571..580
x=573, y=262..269
y=14, x=574..578
y=858, x=580..588
x=518, y=1406..1414
y=8, x=535..557
y=1377, x=529..533
x=571, y=969..971
x=585, y=1586..1611
x=593, y=830..840
x=510, y=1452..1464
x=583, y=609..622
x=525, y=1563..1573
y=122, x=551..553
x=533, y=1543..1555
y=1783, x=556..558
x=502, y=1547..1556
y=187, x=569..579
y=1274, x=562..568
x=552, y=917..931
x=563, y=1232..1243
x=496, y=463..478
y=1594, x=496..502
x=566, y=1070..1074
x=525, y=354..367
y=178, x=563..566
x=500, y=696..697
x=501, y=321..325
x=561, y=494..505
y=712, x=490..509
x=553, y=457..470
x=565, y=947..950
x=513, y=744..763
x=508, y=1062..1069
x=498, y=1657..1673
y=1895, x=567..592
x=566, y=992..999
x=492, y=257..267
x=491, y=1399..1406
x=531, y=1794..1808
x=528, y=1752..1762
x=565, y=920..927
y=1314, x=526..552
x=486, y=1061..1069
y=82, x=517..543
x=500, y=1399..1406
x=531, y=1326..1328
x=527, y=586..588
x=563, y=588..600
x=490, y=706..712
y=1494, x=560..565
x=498, y=1794..1796
y=384, x=532..542
x=552, y=1637..1640
x=495, y=745..756
y=803, x=556..559
x=577, y=1886..1892
y=1162, x=500..533
y=1334, x=510..526
x=514, y=238..250
x=548, y=263..269
y=1054, x=522..529
x=507, y=771..785
x=579, y=183..187
x=500, y=1508..1512
y=1681, x=548..560
y=1726, x=512..527
y=968, x=538..541
x=530, y=1244..1246
x=530, y=1223..1233
x=590, y=1649..1673
x=570, y=1751..1763
y=1571, x=483..496
x=493, y=10..21
x=562, y=1311..1322
y=212, x=506..524
x=582, y=1311..1322
x=486, y=1832..1845
x=505, y=1361..1368
x=580, y=315..329
x=512, y=427..433
x=571, y=1841..1852
x=587, y=1809..1820
y=574, x=579..585
x=543, y=1364..1374
y=1322, x=562..582
x=572, y=1886..1892
y=859, x=556..573
x=525, y=493..500
x=564, y=862..872
y=1082, x=535..555
x=549, y=1068..1078
y=158, x=549..555
x=506, y=1077..1087
x=554, y=754..765
y=622, x=563..583
x=579, y=1808..1820
y=1611, x=566..585
x=563, y=175..178
x=496, y=1886..1896
x=547, y=1688..1699
x=500, y=1094..1102
x=550, y=1411..1414
x=488, y=1239..1260
x=568, y=1265..1274
x=572, y=1679..1705
x=521, y=1406..1414
x=536, y=1343..1356
y=54, x=542..544
x=559, y=1443..1460
x=535, y=1430..1440
x=547, y=1930..1932
x=579, y=887..889
x=586, y=1347..1374
y=331, x=484..507
x=512, y=633..639
x=572, y=1070..1074
x=515, y=1604..1617
y=230, x=535..537
x=522, y=1488..1501
x=545, y=1442..1460
x=488, y=1833..1845
y=1627, x=548..552
y=1216, x=519..524
x=562, y=1151..1162
x=557, y=45..65
x=524, y=743..760
y=1328, x=531..533
x=524, y=256..264
x=499, y=29..41
y=846, x=526..540
y=305, x=515..517
y=374, x=489..513
x=503, y=1835..1844
y=146, x=509..551
y=1821, x=490..505
x=562, y=1264..1274
x=545, y=18..23
x=527, y=1409..1419
x=517, y=70..82
x=483, y=414..419
x=484, y=1474..1477
x=521, y=276..285
x=531, y=1280..1288
x=562, y=1345..1361
y=993, x=505..508
x=520, y=911..930
y=685, x=512..529
x=551, y=161..172
x=540, y=820..846
x=533, y=494..500
x=563, y=1198..1211
x=545, y=226..237
y=713, x=515..521
x=530, y=1096..1098
y=1061, x=543..546
x=520, y=717..721
x=497, y=1703..1719
x=505, y=1814..1821
x=512, y=1580..1591
x=575, y=438..448
y=833, x=497..514
y=1444, x=566..573
y=1834, x=510..512
x=515, y=1109..1123
y=1374, x=543..548
x=542, y=687..705
y=1512, x=565..570
x=534, y=15..25
x=531, y=322..325
x=488, y=12..32
x=557, y=6..8
x=535, y=5..8
x=560, y=1674..1681
y=893, x=513..534
y=25, x=531..534
x=536, y=1686..1696
x=539, y=1259..1271
y=647, x=502..521
x=521, y=674..681
x=549, y=1002..1015
x=540, y=901..911
y=1123, x=515..534
x=539, y=1241..1252
x=578, y=522..545
x=581, y=198..207
y=233, x=535..537
x=510, y=1491..1498
y=367, x=525..547
x=570, y=480..489
x=533, y=1280..1288
x=580, y=1139..1156
x=493, y=993..1009
y=23, x=545..565
y=1805, x=503..524
x=590, y=1097..1116
x=549, y=1709..1718
x=501, y=938..944
x=496, y=1570..1571
x=521, y=1530..1534
x=582, y=813..823
x=496, y=1742..1760
x=502, y=1027..1032
x=570, y=795..806
x=569, y=184..187
y=955, x=530..588
x=527, y=1388..1397
y=1514, x=527..532
x=528, y=1733..1742
x=522, y=1023..1036
y=1568, x=560..574
y=1677, x=524..545
y=820, x=573..576
x=529, y=1435..1437
x=504, y=257..267
x=558, y=813..823
x=589, y=898..912
x=557, y=991..999
x=502, y=630..647
y=62, x=485..530
x=495, y=1372..1389
x=505, y=826..829
x=502, y=1586..1594
x=518, y=424..447
y=1573, x=525..544
x=555, y=1149..1159
x=573, y=1352..1360
x=511, y=1112..1125
y=847, x=544..550
y=122, x=494..516
x=526, y=819..846
x=524, y=1023..1036
x=499, y=1487..1501
x=530, y=49..62
x=559, y=733..742
x=559, y=792..803
y=1764, x=579..581
x=561, y=563..576
x=493, y=1632..1642
x=547, y=354..367
y=531, x=541..558
x=569, y=633..643
x=505, y=772..785
x=527, y=192..195
y=289, x=512..530
x=545, y=1257..1268
x=486, y=1238..1260
y=1074, x=566..572
x=524, y=1791..1805
x=580, y=845..858
x=563, y=94..104
y=652, x=577..582
y=1862, x=493..500
x=536, y=1646..1658
x=502, y=789..813
y=1886, x=572..577
y=992, x=515..539
y=610, x=498..514
x=510, y=1793..1797
y=486, x=562..564
x=546, y=427..436
x=554, y=694..706
x=521, y=712..713
x=504, y=340..349
x=536, y=1626..1636
x=572, y=695..706
y=1464, x=510..530
x=520, y=1157..1159
y=1833, x=539..561
x=534, y=1605..1617
y=721, x=499..520
y=1667, x=489..491
x=570, y=566..579
y=1482, x=524..544
x=570, y=1149..1153
y=325, x=564..570
x=511, y=1509..1512
x=523, y=219..230
x=535, y=1071..1082
x=515, y=711..713
x=542, y=1092..1102
x=489, y=1664..1667
y=1797, x=510..517
x=514, y=217..226
x=554, y=1247..1253
x=524, y=1624..1632
y=1796, x=492..498
y=1414, x=546..550
x=532, y=380..384
x=546, y=668..680
y=1416, x=558..564
x=573, y=845..859
x=506, y=1834..1844
y=1059, x=516..535
x=575, y=1638..1640
x=520, y=1283..1293
y=840, x=575..593
x=515, y=177..191
x=485, y=994..1009
x=556, y=1168..1170
y=1343, x=577..589
y=723, x=569..583
x=543, y=1056..1061
x=584, y=1214..1228
x=545, y=1668..1677
x=554, y=1930..1932
x=581, y=965..975
x=554, y=1645..1658
x=529, y=1047..1054
y=1293, x=520..539
x=553, y=439..448
y=1892, x=572..577
y=309, x=557..569
x=526, y=1777..1782
x=508, y=1552..1567
x=540, y=1199..1211
x=537, y=1774..1785
x=574, y=9..14
x=534, y=1870..1888
y=1419, x=510..527
x=519, y=1580..1591
y=1640, x=552..575
x=569, y=307..309
x=527, y=1714..1726
y=1069, x=486..508
x=521, y=1848..1865
x=577, y=1332..1343
x=516, y=462..478
x=585, y=416..430
y=1699, x=530..547
x=566, y=1587..1611
y=1034, x=562..569
y=745, x=546..565
y=1922, x=502..528
y=1896, x=496..503
x=565, y=692..703
x=483, y=1657..1673
y=389, x=517..555
y=781, x=580..583
x=530, y=1689..1699
x=558, y=542..554
x=518, y=1773..1785
x=557, y=1522..1535
y=405, x=486..498
x=506, y=203..212
x=505, y=1633..1642
x=544, y=181..197
x=541, y=114..127
y=1406, x=491..500
y=600, x=544..563
x=556, y=1844..1848
y=1102, x=525..542
x=558, y=1260..1271
y=292, x=500..507
x=548, y=717..721
y=1246, x=530..532
y=139, x=527..534
x=584, y=523..545
x=517, y=1072..1094
y=1244, x=530..532
y=1534, x=521..525
y=1594, x=507..525
y=142, x=568..583
x=515, y=981..992
y=1512, x=500..511
y=806, x=551..570
x=558, y=563..576
x=532, y=1244..1246
y=872, x=564..566
y=545, x=578..584
x=510, y=1324..1334
x=509, y=705..712
x=533, y=1327..1328
y=490, x=540..547
y=875, x=557..575
x=557, y=337..346
y=1849, x=580..591
y=927, x=565..567
x=585, y=567..574
x=494, y=1704..1719
y=447, x=501..518
x=485, y=1279..1288
y=1253, x=545..554
x=497, y=912..924
y=554, x=526..552
y=226, x=510..514
y=950, x=565..575
x=512, y=587..588
y=1032, x=502..504
y=1098, x=530..532
x=569, y=1029..1034
x=582, y=634..652
y=1390, x=538..544
x=549, y=1574..1595
y=1540, x=578..585
x=536, y=1670..1674
x=535, y=1523..1535
x=488, y=1077..1087
x=581, y=361..373
x=502, y=1867..1870
x=503, y=1790..1805
y=252, x=561..577
x=524, y=52..55
x=575, y=37..57
x=565, y=1510..1512
x=504, y=220..230
y=1611, x=520..524
x=546, y=1222..1233
x=574, y=1617..1631
x=500, y=1838..1862
x=524, y=1611..1614
x=526, y=542..554
x=549, y=759..771
y=321, x=499..501
y=554, x=558..568
y=250, x=483..514
y=264, x=522..524

Part 1:
<Query Kind="Program">
  <Namespace>System.Drawing</Namespace>
</Query>

bool[,] Map;
int X0;
int Y0;
int Width;
int Height;

enum Water { None=0, Flow=1, Rest=2 }
Water[,] water;

void Main()
{
	Load(File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"17_input.txt")));
	//DumpMap();

	water[500, 0] = Water.Flow;
	int w = 0;
	Water[,] wold = new Water[Width, Height];
	for (int gen = 0; ; gen++)
	{
		Tick();
		//Util.ClearResults();
		//DumpMap();
		int w0 = WaterCount();
		if (w == w0 && WaterEquals(wold,water)) { $"{w0} after {gen} generations".Dump(); DumpMap(); /* DumpMapASCII(); */ return; }
		w=w0;
		for (int y = 0; y < Height; y++) for (int x = 0; x < Width; x++) wold[x,y]=water[x,y];
	}
	
	
}

bool WaterEquals(Water[,] a, Water[,] b)
{
	for (int y = 0; y < Height; y++) for (int x = 0; x < Width; x++) if (a[x,y] != b[x,y])return false;
	return true;
}

int WaterCount()
{
	int c = 0;
	for (int y = Y0; y < Height; y++) for (int x = 0; x < Width; x++) if (water[x,y] != Water.None)c++;
	return c;
}

void Tick()
{
	for (int xx = 0; xx < Width; xx++) for (int yy = 0; yy < Height; yy++) if (water[xx,yy]==Water.Flow) water[xx,yy]=Water.None;
	
	var next = new Stack<(int,int)>();
	next.Push( (500, 0) );
	while (next.Any())
	{
		(var x, var y) = next.Pop();
		
		if (y==Height-1) continue;
		
		if (Map[x,y+1] || water[x,y+1]==Water.Rest)
		{
			var l = false;
			var r = false;
			if (!Map[x - 1, y] && water[x - 1, y] == Water.None) { l = true; water[x - 1, y] = Water.Flow; next.Push((x - 1, y)); }
			if (!Map[x + 1, y] && water[x + 1, y] == Water.None) { r = true; water[x + 1, y] = Water.Flow; next.Push((x + 1, y)); }

			if (!l && !r)
			{
				var solid = true;
				for (int xx = x; solid; xx++)
				{
					if (water[xx, y] == Water.None) { solid = false; break; }
					if (!(Map[xx, y + 1] || water[xx, y + 1] == Water.Rest)) { solid = false; break; }
					if (Map[xx + 1, y]) break;
				}
				for (int xx = x; solid; xx--)
				{
					if (water[xx, y] == Water.None) { solid = false; break; }
					if (!(Map[xx, y + 1] || water[xx, y + 1] == Water.Rest)) { solid = false; break; }
					if (Map[xx - 1, y]) break;
				}

				if (solid)
				{
					for (int xx = x; !Map[xx, y]; xx++) { Assert(water[xx, y] != Water.None); water[xx, y] = Water.Rest; }
					for (int xx = x; !Map[xx, y]; xx--) { Assert(water[xx, y] != Water.None); water[xx, y] = Water.Rest; }
				}
			}
		}
		else
		{
			water[x, y+1] = Water.Flow;
			next.Push( (x,y+1) );
		}
	}
}

void Load(string[] v)
{
	var data = new List<(int,int,int,int)>();
	
	for (int i = 0; i < v.Length; i++)
	{
		var a = v[i].Split(',')[0].Trim();
		var b = v[i].Split(',')[1].Trim();

		if (b.StartsWith("x")) { var t = b; b = a; a = t; }

		a = a.Substring(2);
		b = b.Substring(2);

		var a1 = int.Parse(a.Contains("..") ? a.Split('.')[0] : a);
		var a2 = int.Parse(a.Contains("..") ? a.Split('.')[2] : a);
		var b1 = int.Parse(b.Contains("..") ? b.Split('.')[0] : b);
		var b2 = int.Parse(b.Contains("..") ? b.Split('.')[2] : b);
		
		data.Add( (a1,a2,b1,b2) );
	}

	Width  = data.Max(m => Math.Max(m.Item1, m.Item2)) + 1;
	Height = data.Max(m => Math.Max(m.Item3, m.Item4)) + 1;
	X0     = data.Min(m => Math.Min(m.Item1, m.Item2)) - 1;
	Y0     = data.Min(m => Math.Min(m.Item3, m.Item4));
	Map = new bool[Width, Height];

	foreach ((var x1, var x2, var y1, var y2) in data)
	{
		for (int x = x1; x <= x2; x++) for (int y = y1; y <= y2; y++) Map[x, y] = true;
	}

	water = new Water[Width, Height];
}

void DumpMap()
{
	Bitmap bmp = new Bitmap(Width - X0, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
	using (Graphics g = Graphics.FromImage(bmp))
	{
		g.Clear(Color.Wheat);
		for (int y = 0; y < Height; y++)
		{
			for (int x = X0; x < Width; x++)
			{
				if (x == (500 - X0) && y == 0) g.FillRectangle(Brushes.DarkBlue, x - X0, y, 1, 1);
				else if (Map[x, y]) g.FillRectangle(Brushes.Black, x - X0, y, 1, 1);
				else if (water[x, y] == Water.Flow) g.FillRectangle(Brushes.Cyan, x - X0, y, 1, 1);
				else if (water[x, y] == Water.Rest) g.FillRectangle(Brushes.LightBlue, x - X0, y, 1, 1);
			}
		}
	}
	bmp.Dump();
}

void DumpMapASCII()
{
	StringBuilder b = new StringBuilder();
	for (int y = 0; y < Height; y++)
	{
		for (int x = 0; x < Width; x++)
		{
			if (x == (500) && y == 0) b.Append('+');
			else if (Map[x, y]) b.Append('#');
			else if (water[x, y] == Water.Flow) b.Append('|');
			else if (water[x, y] == Water.Rest) b.Append('~');
			else b.Append('.');
		}
		b.AppendLine();
	}
	b.ToString().Dump();
}

void Assert(bool b) { if (!b) throw new Exception(); }
Result: 33004

Part 2:
<Query Kind="Program">
  <Namespace>System.Drawing</Namespace>
</Query>

bool[,] Map;
int X0;
int Y0;
int Width;
int Height;

enum Water { None=0, Flow=1, Rest=2 }
Water[,] water;

void Main()
{
	Load(File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"17_input.txt")));
	//DumpMap();

	water[500, 0] = Water.Flow;
	int w = 0;
	Water[,] wold = new Water[Width, Height];
	for (int gen = 0; ; gen++)
	{
		Tick();
		//Util.ClearResults();
		//DumpMap();
		int w0 = WaterCount();
		if (w == w0 && WaterEquals(wold, water)) { $"{StillWaterCount()} after {gen} generations (all = {w0})".Dump(); DumpMap(); /* DumpMapASCII(); */ return; }
		w=w0;
		for (int y = 0; y < Height; y++) for (int x = 0; x < Width; x++) wold[x,y]=water[x,y];
	}
	
	
}

bool WaterEquals(Water[,] a, Water[,] b)
{
	for (int y = 0; y < Height; y++) for (int x = 0; x < Width; x++) if (a[x,y] != b[x,y])return false;
	return true;
}

int WaterCount()
{
	int c = 0;
	for (int y = Y0; y < Height; y++) for (int x = 0; x < Width; x++) if (water[x, y] != Water.None) c++;
	return c;
}

int StillWaterCount()
{
	int c = 0;
	for (int y = Y0; y < Height; y++) for (int x = 0; x < Width; x++) if (water[x, y] == Water.Rest) c++;
	return c;
}

void Tick()
{
	for (int xx = 0; xx < Width; xx++) for (int yy = 0; yy < Height; yy++) if (water[xx,yy]==Water.Flow) water[xx,yy]=Water.None;
	
	var next = new Stack<(int,int)>();
	next.Push( (500, 0) );
	while (next.Any())
	{
		(var x, var y) = next.Pop();
		
		if (y==Height-1) continue;
		
		if (Map[x,y+1] || water[x,y+1]==Water.Rest)
		{
			var l = false;
			var r = false;
			if (!Map[x - 1, y] && water[x - 1, y] == Water.None) { l = true; water[x - 1, y] = Water.Flow; next.Push((x - 1, y)); }
			if (!Map[x + 1, y] && water[x + 1, y] == Water.None) { r = true; water[x + 1, y] = Water.Flow; next.Push((x + 1, y)); }

			if (!l && !r)
			{
				var solid = true;
				for (int xx = x; solid; xx++)
				{
					if (water[xx, y] == Water.None) { solid = false; break; }
					if (!(Map[xx, y + 1] || water[xx, y + 1] == Water.Rest)) { solid = false; break; }
					if (Map[xx + 1, y]) break;
				}
				for (int xx = x; solid; xx--)
				{
					if (water[xx, y] == Water.None) { solid = false; break; }
					if (!(Map[xx, y + 1] || water[xx, y + 1] == Water.Rest)) { solid = false; break; }
					if (Map[xx - 1, y]) break;
				}

				if (solid)
				{
					for (int xx = x; !Map[xx, y]; xx++) { Assert(water[xx, y] != Water.None); water[xx, y] = Water.Rest; }
					for (int xx = x; !Map[xx, y]; xx--) { Assert(water[xx, y] != Water.None); water[xx, y] = Water.Rest; }
				}
			}
		}
		else
		{
			water[x, y+1] = Water.Flow;
			next.Push( (x,y+1) );
		}
	}
}

void Load(string[] v)
{
	var data = new List<(int,int,int,int)>();
	
	for (int i = 0; i < v.Length; i++)
	{
		var a = v[i].Split(',')[0].Trim();
		var b = v[i].Split(',')[1].Trim();

		if (b.StartsWith("x")) { var t = b; b = a; a = t; }

		a = a.Substring(2);
		b = b.Substring(2);

		var a1 = int.Parse(a.Contains("..") ? a.Split('.')[0] : a);
		var a2 = int.Parse(a.Contains("..") ? a.Split('.')[2] : a);
		var b1 = int.Parse(b.Contains("..") ? b.Split('.')[0] : b);
		var b2 = int.Parse(b.Contains("..") ? b.Split('.')[2] : b);
		
		data.Add( (a1,a2,b1,b2) );
	}

	Width  = data.Max(m => Math.Max(m.Item1, m.Item2)) + 1;
	Height = data.Max(m => Math.Max(m.Item3, m.Item4)) + 1;
	X0     = data.Min(m => Math.Min(m.Item1, m.Item2)) - 1;
	Y0     = data.Min(m => Math.Min(m.Item3, m.Item4));
	Map = new bool[Width, Height];

	foreach ((var x1, var x2, var y1, var y2) in data)
	{
		for (int x = x1; x <= x2; x++) for (int y = y1; y <= y2; y++) Map[x, y] = true;
	}

	water = new Water[Width, Height];
}

void DumpMap()
{
	Bitmap bmp = new Bitmap(Width - X0, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
	using (Graphics g = Graphics.FromImage(bmp))
	{
		g.Clear(Color.Wheat);
		for (int y = 0; y < Height; y++)
		{
			for (int x = X0; x < Width; x++)
			{
				if (x == (500 - X0) && y == 0) g.FillRectangle(Brushes.DarkBlue, x - X0, y, 1, 1);
				else if (Map[x, y]) g.FillRectangle(Brushes.Black, x - X0, y, 1, 1);
				else if (water[x, y] == Water.Flow) g.FillRectangle(Brushes.Cyan, x - X0, y, 1, 1);
				else if (water[x, y] == Water.Rest) g.FillRectangle(Brushes.LightBlue, x - X0, y, 1, 1);
			}
		}
	}
	bmp.Dump();
}

void DumpMapASCII()
{
	StringBuilder b = new StringBuilder();
	for (int y = 0; y < Height; y++)
	{
		for (int x = 0; x < Width; x++)
		{
			if (x == (500) && y == 0) b.Append('+');
			else if (Map[x, y]) b.Append('#');
			else if (water[x, y] == Water.Flow) b.Append('|');
			else if (water[x, y] == Water.Rest) b.Append('~');
			else b.Append('.');
		}
		b.AppendLine();
	}
	b.ToString().Dump();
}

void Assert(bool b) { if (!b) throw new Exception(); }
Result: 23294


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript