AoC 2022 D12: Hill Climbing Algorithm
| Problem statement | Source code | Tags: BFS/DFSMaze
← Previous Back to AoC Index Next →
This is a shortest path problem with equal edge weights, so we can just run BFS. Each node (cell) is connected to its neighbors if the height difference is at most 1.
I parse the input grid into a Map (Int, Int) Int, storing the heights of each cell. This part isn't very interesting so I'll skip it. Anyway, part 1 has a single starting point and part 2 has multiple starting points, but the BFS logic works the same. I just enqueue all starting points at the beginning of the BFS, where the queue keeps track of the cell's coordinates and the distance from the start. The BFS stops when we reach the end cell or the queue is depleted.
This is my first time implementing BFS in a functional language. It turned out to be not more complicated than the loop version.