Related puzzle: Pathfinder (available on Puzzle Picnic)
Background
A robot is standing on a cell of a rectangular grid, where each cell has one or more gems except for the one with the robot. The robot can move to a horizontally or vertically adjacent cell in one step, and it picks up one gem from the cell it steps on.
The robot is programmed with which cell to choose for its next step, based on the number of gems each adjacent cell contains. The program is in the form of a > b > c > ... > z
, which means that the robot prefers to move into the cell with a
gems, followed by one with b
gems, and so on. Any such program contains positive integers from 1 to n
exactly once, where n
is the length of the program. The robot will never move into any cell with no gems.
If there are multiple highest-priority cells around the robot, or all the cells around it are empty, it will get stuck at that point.
For example, if its program is 1 > 3 > 2
(meaning, it prefers the cell with only one gem the most, followed by the one with 3 gems, and then 2 gems) and the current state looks like this (R is the robot, the numbers are gems):
0 3 2
2 R 1
1 2 3
Then it will choose the cell on its right because 1 has the highest priority. Assuming the cell with R is empty, it will continue moving down, left, left, right, right, then get stuck since the cells around it have no gems left.
0 3 2 0 3 2 0 3 2 0 3 2 0 3 2 0 3 2 0 3 2
2 R 1 --> 2 0 R --> 2 0 0 --> 2 0 0 --> 2 0 0 --> 2 0 0 --> 2 0 0
1 2 3 1 2 3 1 2 R 1 R 2 R 1 2 0 R 2 0 0 R
R D L L R R
Using the 1 > 3 > 2
program, it will get stuck at any of the following cases:
0 R 0 # all adjacent cells are empty
2 0 1
1 2 2
3 1 0 # there are two 1s around the robot, so it can't decide where to go
2 R 1
1 0 2
Challenge
Given the initial state of the entire grid and the robot's program, find the position (horizontal and vertical coordinates) in the grid where the robot will eventually get stuck.
The initial state contains the numbers between 1 and 9 inclusive, except for a single 0 which denotes the initial position of the robot. The program is guaranteed to contain all positive numbers that appear in the grid, and the program is always valid (contains each of 1 to the maximum value on the grid exactly once).
You can take the input (grid and program) in any suitable format, and output the coordinates in any sensible way.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
In the example I/O provided, the program has the highest precedence coming first, and the coordinates are 1-based.
Grid:
0 2 1
3 3 2
1 1 3
Program: 3 1 2
Result: 2nd row, 3rd column (Path: DRDRUUD)
Grid: (one row)
0 3 2 3 1 4 4
Program: 4 2 3 1
Result: 1st row, 7th column (Path: RRLRRRRRLRLRLR)
Grid:
0 1 3
3 1 3
2 1 2
Program: 3 1 2
Result: 3rd row, 3rd column (Path: DRRULRDD)
Grid:
2 3 1 2 1
2 0 3 2 2
1 1 3 1 1
Program: 1 3 2
Result: 2nd row, 4th column (Path: DLUUDURRDDRRUULD)
Same grid as above, Program: 3 2 1
Result: 2nd row, 2nd column (Stuck at zero moves)