Advent of Code 2023 - Day 16The Floor Will Be Lava
| Problem statement | Source code | Tags: Grid walkingBFS/DFS
Part 1
I represent each direction as a number: left = 1, right = 2, up = 3, down = 4. Then I know how to turn the direction given each kind of cell and the current light direction:
Now I can just do a DFS from the starting position. Whenever a light in a certain direction hits a cell, I mark that cell as visited in that direction, and schedule to visit its neighbors. The answer is the number of cells that have been visited in at least one direction.
Part 1's answer is just energize(mat, 1, 1, 2).
Part 2
I brute-forced all edge positions.
This takes about 8 seconds to run, which IMO is acceptable for R. I could probably optimize it by memoizing the remaining path from each cell and direction, but I don't think it's worth the effort, especially since I have to think about deduplication of the energized cells, and memoization itself is a pain in R without hashmaps.