The turtle wants to move along the grid to get to his food. He wants to know how many moves it will take for him to get there.
As well since he is slow he has teleporters set up around his domain that he will utilize if it shortens his path. Or avoid them if it lengthens his path.
Meet the turtle
π’
The turtle lives on a grid $$\begin{matrix} X&X&X&X&X\\ X&X&X&X&X\\ X&X&π’&X&X\\ X&X&X&X&X\\ X&X&X&X&X\\ \end{matrix}$$ The turtle can move to any adjacent square... $$\begin{matrix} X&X&X&X&X\\ X&\nwarrow&\uparrow&\nearrow&X\\ X&\leftarrow&π’&\rightarrow&X\\ X&\swarrow&\downarrow&\searrow&X\\ X&X&X&X&X\\ \end{matrix}$$
However, the turtle cannot move to a square with a mountain $$\begin{matrix} X&π&X&X&X\\ X&\nwarrow&\uparrow&\nearrow&X\\ X&π&π’&\rightarrow&X\\ X&π&\downarrow&\searrow&X\\ X&π&X&X&X\\ \end{matrix}$$
The Turtle wants to eat his Strawberry, and would like to know how long it will take to get to his Strawberry $$\begin{matrix} X&π&π\\ π’&π&X\\ X&π&X\\ X&X&X\\ \end{matrix}$$ This example would take the turtle \$5\$ turns $$\begin{matrix} X&π&π\\ \downarrow&π&\uparrow\\ \searrow&π&\uparrow\\ X&\nearrow&X\\ \end{matrix}$$ Luckily, the Turtle found a teleporter! There are two teleports on the grid that map to each other. Stepping on the teleporter immediately moves the turtle to the corresponding teleporter. Teleporters are very unstable and after using them once, they disapear and are no longer useable. $$\begin{matrix} π΅&π&π\\ π’&π&π΄\\ X&π&X\\ X&X&X\\ \end{matrix}$$ It is now faster for the turtle to move up twice. Now the turtles shortest path is \$2\$ $$\begin{matrix} π΅&π&π’\\ \uparrow&π&π΄\uparrow\\ X&π&X\\ X&X&X\\ \end{matrix}$$
The challenge
Given an initial grid configuration output the number of moves it will take the turtle to reach his strawberry.
Rules
You may assume that the input grid has a solution
Each grid will only have one
strawberry
and twoportals
and oneturtle
The input grid may be entered in any convenient format
You should treat
teleporters
are single use itemsThe turn that the turtle moves onto a
teleporter
square he is already on the correspondingteleporter
. He never moves onto ateleporter
and stays there for a moveThe shortest path does not need to make use of the portal
The turtle cannot pass into mountain tiles
You may use any ASCII character or integer to represent
mountains
,turtle
,empty grid square
,strawberry
You may use either the same character or two different ASCII characters or integers to represent the
teleporter
pairsA grid can have more than one path with the same shortest path length
This is code-golf
Clarifications to Rules
- You should treat
teleporters
are single use items.
Reasoning: It was pointed out that the case of: $$\begin{matrix} π’&X&π΅&X&π\\ π&π&π&π&π&\\ π΄&X&X&X&X \end{matrix}$$
Could be only solved by entering and exiting the portals twice. At the time of making this clarification both solutions acted by assuming they were either single use, or there was no reason to try previously used squares. To avoid breaking their hard-worked solutions, this seemed the best way account for this set up. Therefore, this would be considered an invalid grid.
Test Cases formatted as lists
[ ['T', 'X', 'X', 'S', 'X'], ['X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X'] ] --> 3
[ ['T', 'M', 'X', 'S', 'X'], ['X', 'M', 'X', 'X', 'X'], ['O', 'X', 'X', 'X', 'O'] ] --> 4
[ ['T', 'M', 'X', 'S', 'O'], ['O', 'M', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X'] ] --> 2
[ ['T', 'M', 'X', 'S', 'X'], ['O', 'M', 'X', 'X', 'X'], ['O', 'X', 'X', 'X', 'X'] ] --> 4
[ ['T', 'M', 'S', 'X', 'O'], ['X', 'M', 'M', 'M', 'M'], ['X', 'X', 'X', 'X', 'O'] ] --> 7
[ ['T', 'X', 'X', 'S', 'X'], ['O', 'M', 'M', 'M', 'X'], ['X', 'X', 'O', 'X', 'X'] ] --> 3
Test Cases formatted for humans
T X X S X
X X X X X
X X X X X --> 3
T M X S X
X M X X X
O X X X O --> 4
T M X S O
O M X X X
X X X X X --> 2
T M X S X
O M X X X
O X X X X --> 4
T M S X O
X M M M M
X X X X O --> 7
T X X S X
O M M M X
X X O X X --> 3
Credits
Design and structure via: Hungry mouse by Arnauld
Proposed Challenges Edit Advice: Kamil-drakari, beefster
TXOXS,MMMMM,OXXXX
? Does the turtle have to move off the other end of the teleporter and go back onto it? What if that's not possible likeTXOXS,MMMMM,OMXXX
? \$\endgroup\$