This separates the Dijkstra implementation into its own generic function.
Whilst doing this we can simplify the code and also save yet more memory!
Still don't get correct results for part 2.
This separates the Dijkstra implementation into its own generic function.
Whilst doing this we can simplify the code and also save yet more memory!
Still don't get correct results for part 2.
Previously we modelled the top row as a connected series of nodes and allowed them to connect to each other directly. This ignored the rule that you have to move directly to the place you want on the top row and then back out of it.
However, that was fine as the lowest cost would imply that. But it does slow down and give more states to consider.
Instead, model the nodes on the top row as being directly connected to the nodes in the second row. Add some rules about when you can move from one to the other and use those.
This reduces the number of states we have to consider in total. And hopefully speeds up the process.
Improve speed by using two sets for State management:
states: Contains all the states we need to visit indexed by state - Cost is ignored.
costs: Contains all the states we need to visit indexed by cost then state.
This then means the lookup for the minimum cost is fast.