falling falling falling

This commit is contained in:
eleith 2022-12-16 10:39:01 -08:00
parent 1941df2515
commit a0462f3490
4 changed files with 542 additions and 0 deletions

163
14/README.md Normal file
View File

@ -0,0 +1,163 @@
--- Day 14: Regolith Reservoir ---
The distress signal leads you to a giant waterfall! Actually, hang on - the signal seems like it's coming from the waterfall itself, and that doesn't make any sense. However, you do notice a little path that leads behind the waterfall.
Correction: the distress signal leads you behind a giant waterfall! There seems to be a large cave system here, and the signal definitely leads further inside.
As you begin to make your way deeper underground, you feel the ground rumble for a moment. Sand begins pouring into the cave! If you don't quickly figure out where the sand is going, you could quickly become trapped!
Fortunately, your familiarity with analyzing the path of falling material will come in handy here. You scan a two-dimensional vertical slice of the cave above you (your puzzle input) and discover that it is mostly air with structures made of rock.
Your scan traces the path of each solid rock structure and reports the x,y coordinates that form the shape of the path, where x represents distance to the right and y represents distance down. Each path appears as a single line of text in your scan. After the first point of each path, each point indicates the end of a straight horizontal or vertical line to be drawn from the previous point. For example:
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9
This scan means that there are two paths of rock; the first path consists of two straight lines, and the second path consists of three straight lines. (Specifically, the first path consists of a line of rock from 498,4 through 498,6 and another line of rock from 498,6 through 496,6.)
The sand is pouring into the cave from point 500,0.
Drawing rock as #, air as ., and the source of the sand as +, this becomes:
4 5 5
9 0 0
4 0 3
0 ......+...
1 ..........
2 ..........
3 ..........
4 ....#...##
5 ....#...#.
6 ..###...#.
7 ........#.
8 ........#.
9 #########.
Sand is produced one unit at a time, and the next unit of sand is not produced until the previous unit of sand comes to rest. A unit of sand is large enough to fill one tile of air in your scan.
A unit of sand always falls down one step if possible. If the tile immediately below is blocked (by rock or sand), the unit of sand attempts to instead move diagonally one step down and to the left. If that tile is blocked, the unit of sand attempts to instead move diagonally one step down and to the right. Sand keeps moving as long as it is able to do so, at each step trying to move down, then down-left, then down-right. If all three possible destinations are blocked, the unit of sand comes to rest and no longer moves, at which point the next unit of sand is created back at the source.
So, drawing sand that has come to rest as o, the first unit of sand simply falls straight down and then stops:
......+...
..........
..........
..........
....#...##
....#...#.
..###...#.
........#.
......o.#.
#########.
The second unit of sand then falls straight down, lands on the first one, and then comes to rest to its left:
......+...
..........
..........
..........
....#...##
....#...#.
..###...#.
........#.
.....oo.#.
#########.
After a total of five units of sand have come to rest, they form this pattern:
......+...
..........
..........
..........
....#...##
....#...#.
..###...#.
......o.#.
....oooo#.
#########.
After a total of 22 units of sand:
......+...
..........
......o...
.....ooo..
....#ooo##
....#ooo#.
..###ooo#.
....oooo#.
...ooooo#.
#########.
Finally, only two more units of sand can possibly come to rest:
......+...
..........
......o...
.....ooo..
....#ooo##
...o#ooo#.
..###ooo#.
....oooo#.
.o.ooooo#.
#########.
Once all 24 units of sand shown above have come to rest, all further sand flows out the bottom, falling into the endless void. Just for fun, the path any new sand takes before falling forever is shown here with ~:
.......+...
.......~...
......~o...
.....~ooo..
....~#ooo##
...~o#ooo#.
..~###ooo#.
..~..oooo#.
.~o.ooooo#.
~#########.
~..........
~..........
~..........
Using your scan, simulate the falling sand. How many units of sand come to rest before sand starts flowing into the abyss below?
Your puzzle answer was 1298.
--- Part Two ---
You realize you misread the scan. There isn't an endless void at the bottom of the scan - there's floor, and you're standing on it!
You don't have time to scan the floor, so assume the floor is an infinite horizontal line with a y coordinate equal to two plus the highest y coordinate of any point in your scan.
In the example above, the highest y coordinate of any point is 9, and so the floor is at y=11. (This is as if your scan contained one extra rock path like -infinity,11 -> infinity,11.) With the added floor, the example above now looks like this:
...........+........
....................
....................
....................
.........#...##.....
.........#...#......
.......###...#......
.............#......
.............#......
.....#########......
....................
<-- etc #################### etc -->
To find somewhere safe to stand, you'll need to simulate falling sand until a unit of sand comes to rest at 500,0, blocking the source entirely and stopping the flow of sand into the cave. In the example above, the situation finally looks like this after 93 units of sand come to rest:
............o............
...........ooo...........
..........ooooo..........
.........ooooooo.........
........oo#ooo##o........
.......ooo#ooo#ooo.......
......oo###ooo#oooo......
.....oooo.oooo#ooooo.....
....oooooooooo#oooooo....
...ooo#########ooooooo...
..ooooo.......ooooooooo..
#########################
Using your scan, simulate the falling sand until the source of the sand becomes blocked. How many units of sand come to rest?
Your puzzle answer was 25585.

163
14/input.txt Normal file
View File

@ -0,0 +1,163 @@
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
432,82 -> 450,82 -> 450,81
466,59 -> 466,61 -> 461,61 -> 461,66 -> 480,66 -> 480,61 -> 472,61 -> 472,59
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
427,88 -> 427,92 -> 423,92 -> 423,98 -> 440,98 -> 440,92 -> 432,92 -> 432,88
480,42 -> 480,43 -> 489,43 -> 489,42
489,28 -> 489,31 -> 484,31 -> 484,37 -> 500,37 -> 500,31 -> 492,31 -> 492,28
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
403,121 -> 403,125 -> 395,125 -> 395,130 -> 412,130 -> 412,125 -> 406,125 -> 406,121
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
414,105 -> 418,105
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
427,88 -> 427,92 -> 423,92 -> 423,98 -> 440,98 -> 440,92 -> 432,92 -> 432,88
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
466,59 -> 466,61 -> 461,61 -> 461,66 -> 480,66 -> 480,61 -> 472,61 -> 472,59
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
489,28 -> 489,31 -> 484,31 -> 484,37 -> 500,37 -> 500,31 -> 492,31 -> 492,28
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
491,19 -> 496,19
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
427,88 -> 427,92 -> 423,92 -> 423,98 -> 440,98 -> 440,92 -> 432,92 -> 432,88
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
449,74 -> 453,74
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
513,25 -> 518,25
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
427,88 -> 427,92 -> 423,92 -> 423,98 -> 440,98 -> 440,92 -> 432,92 -> 432,88
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
480,42 -> 480,43 -> 489,43 -> 489,42
403,121 -> 403,125 -> 395,125 -> 395,130 -> 412,130 -> 412,125 -> 406,125 -> 406,121
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
498,19 -> 503,19
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
466,59 -> 466,61 -> 461,61 -> 461,66 -> 480,66 -> 480,61 -> 472,61 -> 472,59
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
502,22 -> 507,22
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
403,121 -> 403,125 -> 395,125 -> 395,130 -> 412,130 -> 412,125 -> 406,125 -> 406,121
509,22 -> 514,22
506,25 -> 511,25
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
427,88 -> 427,92 -> 423,92 -> 423,98 -> 440,98 -> 440,92 -> 432,92 -> 432,88
499,25 -> 504,25
505,19 -> 510,19
495,22 -> 500,22
443,78 -> 447,78
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
403,121 -> 403,125 -> 395,125 -> 395,130 -> 412,130 -> 412,125 -> 406,125 -> 406,121
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
488,22 -> 493,22
417,103 -> 421,103
403,121 -> 403,125 -> 395,125 -> 395,130 -> 412,130 -> 412,125 -> 406,125 -> 406,121
494,16 -> 499,16
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
446,76 -> 450,76
489,28 -> 489,31 -> 484,31 -> 484,37 -> 500,37 -> 500,31 -> 492,31 -> 492,28
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
466,59 -> 466,61 -> 461,61 -> 461,66 -> 480,66 -> 480,61 -> 472,61 -> 472,59
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
501,16 -> 506,16
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
466,59 -> 466,61 -> 461,61 -> 461,66 -> 480,66 -> 480,61 -> 472,61 -> 472,59
430,84 -> 430,85 -> 436,85
426,105 -> 430,105
452,70 -> 452,71 -> 465,71 -> 465,70
485,25 -> 490,25
489,28 -> 489,31 -> 484,31 -> 484,37 -> 500,37 -> 500,31 -> 492,31 -> 492,28
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
455,78 -> 459,78
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
466,59 -> 466,61 -> 461,61 -> 461,66 -> 480,66 -> 480,61 -> 472,61 -> 472,59
403,121 -> 403,125 -> 395,125 -> 395,130 -> 412,130 -> 412,125 -> 406,125 -> 406,121
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
423,103 -> 427,103
480,42 -> 480,43 -> 489,43 -> 489,42
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
452,76 -> 456,76
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
430,84 -> 430,85 -> 436,85
452,70 -> 452,71 -> 465,71 -> 465,70
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
395,136 -> 399,136
420,101 -> 424,101
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
389,136 -> 393,136
427,88 -> 427,92 -> 423,92 -> 423,98 -> 440,98 -> 440,92 -> 432,92 -> 432,88
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
427,88 -> 427,92 -> 423,92 -> 423,98 -> 440,98 -> 440,92 -> 432,92 -> 432,88
398,139 -> 402,139
489,28 -> 489,31 -> 484,31 -> 484,37 -> 500,37 -> 500,31 -> 492,31 -> 492,28
466,59 -> 466,61 -> 461,61 -> 461,66 -> 480,66 -> 480,61 -> 472,61 -> 472,59
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
452,70 -> 452,71 -> 465,71 -> 465,70
420,105 -> 424,105
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
449,78 -> 453,78
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
489,28 -> 489,31 -> 484,31 -> 484,37 -> 500,37 -> 500,31 -> 492,31 -> 492,28
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
492,25 -> 497,25
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
392,133 -> 396,133
497,13 -> 502,13
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
432,82 -> 450,82 -> 450,81
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
386,139 -> 390,139
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
368,165 -> 368,164 -> 368,165 -> 370,165 -> 370,164 -> 370,165 -> 372,165 -> 372,163 -> 372,165 -> 374,165 -> 374,161 -> 374,165 -> 376,165 -> 376,157 -> 376,165 -> 378,165 -> 378,164 -> 378,165 -> 380,165 -> 380,162 -> 380,165 -> 382,165 -> 382,163 -> 382,165 -> 384,165 -> 384,158 -> 384,165
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
392,139 -> 396,139
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
403,121 -> 403,125 -> 395,125 -> 395,130 -> 412,130 -> 412,125 -> 406,125 -> 406,121
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
470,56 -> 470,54 -> 470,56 -> 472,56 -> 472,46 -> 472,56 -> 474,56 -> 474,49 -> 474,56 -> 476,56 -> 476,49 -> 476,56 -> 478,56 -> 478,54 -> 478,56 -> 480,56 -> 480,46 -> 480,56 -> 482,56 -> 482,52 -> 482,56 -> 484,56 -> 484,53 -> 484,56 -> 486,56 -> 486,47 -> 486,56
489,28 -> 489,31 -> 484,31 -> 484,37 -> 500,37 -> 500,31 -> 492,31 -> 492,28
406,118 -> 406,110 -> 406,118 -> 408,118 -> 408,115 -> 408,118 -> 410,118 -> 410,115 -> 410,118 -> 412,118 -> 412,110 -> 412,118 -> 414,118 -> 414,111 -> 414,118 -> 416,118 -> 416,114 -> 416,118 -> 418,118 -> 418,110 -> 418,118
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152
378,152 -> 378,147 -> 378,152 -> 380,152 -> 380,148 -> 380,152 -> 382,152 -> 382,145 -> 382,152 -> 384,152 -> 384,145 -> 384,152 -> 386,152 -> 386,147 -> 386,152 -> 388,152 -> 388,151 -> 388,152 -> 390,152 -> 390,144 -> 390,152

2
14/sample-input.txt Normal file
View File

@ -0,0 +1,2 @@
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9

214
14/src/index.ts Normal file
View File

@ -0,0 +1,214 @@
import { readFileSync } from 'fs'
const CaveSpaces = {
rock: '#',
empty: '.',
sand: 'o',
source: '+',
infinity: '∞',
} as const
type CaveSpace = typeof CaveSpaces[keyof typeof CaveSpaces]
type CavePoint = [number, number]
type Cave = {
spaces: CaveSpace[][]
source: CavePoint
dimensions: {
top: number
bottom: number
left: number
right: number
}
floor: CaveSpace
sands: CavePoint[]
}
function printCave(cave: Cave) {
console.log(cave.spaces.map((row) => row.join('')).join('\n'))
}
function getCavePoint(cave: Cave, [x, y]: CavePoint): CaveSpace {
if (x - cave.dimensions.left < 0) {
expandCaveLeft(cave)
} else if (x - cave.dimensions.left > cave.dimensions.right - cave.dimensions.left) {
expandCaveRight(cave)
}
const space = cave.spaces?.[y - cave.dimensions.top]?.[x - cave.dimensions.left]
return space ?? CaveSpaces.infinity
}
function setCavePoint(cave: Cave, [x, y]: CavePoint, space: CaveSpace): CavePoint {
cave.spaces[y - cave.dimensions.top][x - cave.dimensions.left] = space
return [x, y]
}
function makeCave(paths: number[][][], source: [number, number], floor: CaveSpace): Cave {
const cave: Cave = {
spaces: [],
dimensions: {
left: source[0],
right: source[0],
top: source[1],
bottom: source[1] + 2,
},
source,
floor,
sands: [],
}
cave.dimensions = paths.reduce((dimensions, path) => {
const xs = path.map(([x]) => x)
const ys = path.map(([, y]) => y)
const minX = Math.min(...xs)
const maxX = Math.max(...xs)
const maxY = Math.max(...ys)
return {
...dimensions,
left: Math.min(dimensions.left, minX - 1),
right: Math.max(dimensions.right, maxX + 1),
bottom: Math.max(dimensions.bottom, maxY + 2),
}
}, cave.dimensions)
cave.spaces = Array(cave.dimensions.bottom - cave.dimensions.top + 1)
.fill('')
.map(() => Array(cave.dimensions.right - cave.dimensions.left + 1).fill(CaveSpaces.empty))
paths.forEach((path) => plotRockPaths(cave, path))
setCavePoint(cave, cave.source, CaveSpaces.source)
makeCaveFloor(cave)
return cave
}
function expandCaveLeft(cave: Cave) {
if (cave.dimensions.left > 0) {
cave.spaces.forEach((row) => {
row.unshift(CaveSpaces.empty)
})
cave.dimensions.left += -1
makeCaveFloor(cave)
}
}
function expandCaveRight(cave: Cave) {
cave.spaces.forEach((row) => {
row.push(CaveSpaces.empty)
})
cave.dimensions.right += 1
makeCaveFloor(cave)
}
function makeCaveFloor(cave: Cave) {
const bottom = cave.dimensions.bottom
const floor = cave.spaces[bottom]
floor.forEach((s, x) => {
floor[x] = s === CaveSpaces.empty ? cave.floor : s
})
}
function isSource(cave: Cave, [x, y]: CavePoint): boolean {
return x === cave.source[0] && y === cave.source[1]
}
function isSandInRow(cave: Cave, row: number): boolean {
return cave.spaces.slice(row)[0].some((space) => space === CaveSpaces.sand)
}
function plotRockPaths(cave: Cave, path: number[][]) {
path.reduce((from, to) => {
const [toX, toY] = to
if (from.length == 2 && to.length == 2) {
const [fromX, fromY] = from
if (fromY === toY) {
for (let x = Math.min(fromX, toX); x <= Math.max(fromX, toX); x++) {
setCavePoint(cave, [x, toY], CaveSpaces.rock)
}
} else {
for (let y = Math.min(fromY, toY); y <= Math.max(fromY, toY); y++) {
setCavePoint(cave, [fromX, y], CaveSpaces.rock)
}
}
}
return to
}, [])
}
function dropSandOneStep(sand: CavePoint, cave: Cave): boolean {
const [x, y] = sand
const tryMoves: CavePoint[] = [
[x, y + 1],
[x - 1, y + 1],
[x + 1, y + 1],
]
const moved = tryMoves.find((move) => {
if (getCavePoint(cave, move) === CaveSpaces.empty) {
setCavePoint(cave, sand, isSource(cave, sand) ? CaveSpaces.source : CaveSpaces.empty)
setCavePoint(cave, move, CaveSpaces.sand)
sand.splice(0, 2, ...move)
return true
}
return false
})
return moved !== undefined
}
function dropSand(cave: Cave) {
const sand = [...cave.source] as CavePoint
cave.sands.push(sand)
setCavePoint(cave, sand, CaveSpaces.sand)
while (dropSandOneStep(sand, cave)) {
// keep dropping
}
}
function solvePart1(lines: string[]): number {
const paths = lines.map((line) => line.split(' -> ').map((point) => point.split(',').map(Number)))
const cave = makeCave(paths, [500, 0], CaveSpaces.empty)
while (!isSandInRow(cave, -1)) {
dropSand(cave)
}
printCave(cave)
return cave.sands.length - 1
}
function solvePart2(lines: string[]): number {
const paths = lines.map((line) => line.split(' -> ').map((point) => point.split(',').map(Number)))
const cave = makeCave(paths, [500, 0], CaveSpaces.rock)
while (!isSandInRow(cave, 0)) {
dropSand(cave)
}
printCave(cave)
return cave.sands.length
}
function run() {
const input = process.argv.slice(2)[0]
const file = readFileSync(input, 'utf8')
const lines = file.split('\n').slice(0, -1)
const solution1 = solvePart1(lines)
console.log(`part1: ${solution1}`)
const solution2 = solvePart2(lines)
console.log(`part2: ${solution2}`)
}
run()