part1 (5301B)
1--- Day 17: Reservoir Research --- 2 3You arrive in the year 18. If it weren't for the coat you got in 1018, you would be very cold: the 4North Pole base hasn't even been constructed. 5 6Rather, it hasn't been constructed [1m[97myet[0m. The Elves are making a little progress, but there's not a 7lot of liquid water in this climate, so they're getting very dehydrated. Maybe there's more 8underground? 9 10You scan a two-dimensional vertical slice of the ground nearby and discover that it is mostly 11[1m[97msand[0m with veins of [1m[97mclay[0m. The scan only provides data with a granularity of [1m[97msquare meters[0m, but it 12should be good enough to determine how much water is trapped there. In the scan, x represents the 13distance to the right, and y represents the distance down. There is also a [1m[97mspring of water[0m near the 14surface at x=500, y=0. The scan identifies [1m[97mwhich square meters are clay[0m (your puzzle input). 15 16For example, suppose your scan shows the following veins of clay: 17 18x=495, y=2..7 19y=7, x=495..501 20x=501, y=3..7 21x=498, y=2..4 22x=506, y=1..2 23x=498, y=10..13 24x=504, y=10..13 25y=13, x=498..504 26 27Rendering clay as #, sand as ., and the water spring as +, and with x increasing to the right and y 28increasing downward, this becomes: 29 30 44444455555555 31 99999900000000 32 45678901234567 33 0 ......+....... 34 1 ............#. 35 2 .#..#.......#. 36 3 .#..#..#...... 37 4 .#..#..#...... 38 5 .#.....#...... 39 6 .#.....#...... 40 7 .#######...... 41 8 .............. 42 9 .............. 4310 ....#.....#... 4411 ....#.....#... 4512 ....#.....#... 4613 ....#######... 47 48The spring of water will produce water [1m[97mforever[0m. Water can move through sand, but is blocked by clay. 49Water [1m[97malways moves down[0m when possible, and spreads to the left and right otherwise, filling space 50that has clay on both sides and falling out otherwise. 51 52For example, if five squares of water are created, they will flow downward until they reach the clay 53and settle there. Water that has come to rest is shown here as ~, while sand through which water has 54passed (but which is now dry again) is shown as |: 55 56......+....... 57......|.....#. 58.#..#.|.....#. 59.#..#.|#...... 60.#..#.|#...... 61.#....|#...... 62.#~~~~~#...... 63.#######...... 64.............. 65.............. 66....#.....#... 67....#.....#... 68....#.....#... 69....#######... 70 71Two squares of water can't occupy the same location. If another five squares of water are created, 72they will settle on the first five, filling the clay reservoir a little more: 73 74......+....... 75......|.....#. 76.#..#.|.....#. 77.#..#.|#...... 78.#..#.|#...... 79.#~~~~~#...... 80.#~~~~~#...... 81.#######...... 82.............. 83.............. 84....#.....#... 85....#.....#... 86....#.....#... 87....#######... 88 89Water pressure does not apply in this scenario. If another four squares of water are created, they 90will stay on the right side of the barrier, and no water will reach the left side: 91 92......+....... 93......|.....#. 94.#..#.|.....#. 95.#..#~~#...... 96.#..#~~#...... 97.#~~~~~#...... 98.#~~~~~#...... 99.#######...... 100.............. 101.............. 102....#.....#... 103....#.....#... 104....#.....#... 105....#######... 106 107At this point, the top reservoir overflows. While water can reach the tiles above the surface of the 108water, it cannot settle there, and so the next five squares of water settle like this: 109 110......+....... 111......|.....#. 112.#..#[1m[97m|[0m|||...#. 113.#..#~~#|..... 114.#..#~~#|..... 115.#~~~~~#|..... 116.#~~~~~#|..... 117.#######|..... 118........|..... 119........|..... 120....#...|.#... 121....#...|.#... 122....#~~~~~#... 123....#######... 124 125Note especially the leftmost |: the new squares of water can reach this tile, but cannot stop there. 126 Instead, eventually, they all fall to the right and settle in the reservoir below. 127 128After 10 more squares of water, the bottom reservoir is also full: 129 130......+....... 131......|.....#. 132.#..#||||...#. 133.#..#~~#|..... 134.#..#~~#|..... 135.#~~~~~#|..... 136.#~~~~~#|..... 137.#######|..... 138........|..... 139........|..... 140....#~~~~~#... 141....#~~~~~#... 142....#~~~~~#... 143....#######... 144 145Finally, while there is nowhere left for the water to settle, it can reach a few more tiles before 146overflowing beyond the bottom of the scanned data: 147 148......+....... (line not counted: above minimum y value) 149......|.....#. 150.#..#||||...#. 151.#..#~~#|..... 152.#..#~~#|..... 153.#~~~~~#|..... 154.#~~~~~#|..... 155.#######|..... 156........|..... 157...|||||||||.. 158...|#~~~~~#|.. 159...|#~~~~~#|.. 160...|#~~~~~#|.. 161...|#######|.. 162...|.......|.. (line not counted: below maximum y value) 163...|.......|.. (line not counted: below maximum y value) 164...|.......|.. (line not counted: below maximum y value) 165 166How many tiles can be reached by the water? [1m[97mTo prevent counting forever[0m, ignore tiles with a y 167coordinate smaller than the smallest y coordinate in your scan data or larger than the largest one. 168Any x coordinate is valid. In this example, the lowest y coordinate given is 1, and the highest is 16913, causing the water spring (in row 0) and the water falling off the bottom of the render (in rows 17014 through infinity) to be ignored. 171 172So, in the example above, counting both water at rest (~) and other sand tiles the water can 173hypothetically reach (|), the total number of tiles the water can reach is [1m[97m57[0m. 174 175[1m[97mHow many tiles can the water reach[0m within the range of y values in your scan? 176 177