A Turtle Finds a Portal
$begingroup$
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
strawberryand twoportalsand oneturtleThe input grid may be entered in any convenient format
You should treat
teleportersare single use itemsThe turn that the turtle moves onto a
teleportersquare he is already on the correspondingteleporter. He never moves onto ateleporterand 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,strawberryYou may use either the same character or two different ASCII characters or integers to represent the
teleporterpairsA grid can have more than one path with the same shortest path length
This is code-golf
Clarifications to Rules
- You should treat
teleportersare 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
General Edit Advice: okx nedla2004 mbomb007
code-golf grid path-finding
$endgroup$
|
show 4 more comments
$begingroup$
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
strawberryand twoportalsand oneturtleThe input grid may be entered in any convenient format
You should treat
teleportersare single use itemsThe turn that the turtle moves onto a
teleportersquare he is already on the correspondingteleporter. He never moves onto ateleporterand 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,strawberryYou may use either the same character or two different ASCII characters or integers to represent the
teleporterpairsA grid can have more than one path with the same shortest path length
This is code-golf
Clarifications to Rules
- You should treat
teleportersare 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
General Edit Advice: okx nedla2004 mbomb007
code-golf grid path-finding
$endgroup$
2
$begingroup$
I think it would be a good idea to add a test case where using the teleporter would make it take longer.
$endgroup$
– Okx
yesterday
$begingroup$
@Okx Creating and adding now.
$endgroup$
– akozi
yesterday
$begingroup$
Edited, thanks.
$endgroup$
– akozi
yesterday
1
$begingroup$
@xnor I feel that this may be to abstract from my original rules. So perhaps it's better to portals a one use item?
$endgroup$
– akozi
yesterday
1
$begingroup$
Related (I think).
$endgroup$
– Charlie
15 hours ago
|
show 4 more comments
$begingroup$
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
strawberryand twoportalsand oneturtleThe input grid may be entered in any convenient format
You should treat
teleportersare single use itemsThe turn that the turtle moves onto a
teleportersquare he is already on the correspondingteleporter. He never moves onto ateleporterand 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,strawberryYou may use either the same character or two different ASCII characters or integers to represent the
teleporterpairsA grid can have more than one path with the same shortest path length
This is code-golf
Clarifications to Rules
- You should treat
teleportersare 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
General Edit Advice: okx nedla2004 mbomb007
code-golf grid path-finding
$endgroup$
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
strawberryand twoportalsand oneturtleThe input grid may be entered in any convenient format
You should treat
teleportersare single use itemsThe turn that the turtle moves onto a
teleportersquare he is already on the correspondingteleporter. He never moves onto ateleporterand 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,strawberryYou may use either the same character or two different ASCII characters or integers to represent the
teleporterpairsA grid can have more than one path with the same shortest path length
This is code-golf
Clarifications to Rules
- You should treat
teleportersare 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
General Edit Advice: okx nedla2004 mbomb007
code-golf grid path-finding
code-golf grid path-finding
edited 6 hours ago
akozi
asked yesterday
akoziakozi
329110
329110
2
$begingroup$
I think it would be a good idea to add a test case where using the teleporter would make it take longer.
$endgroup$
– Okx
yesterday
$begingroup$
@Okx Creating and adding now.
$endgroup$
– akozi
yesterday
$begingroup$
Edited, thanks.
$endgroup$
– akozi
yesterday
1
$begingroup$
@xnor I feel that this may be to abstract from my original rules. So perhaps it's better to portals a one use item?
$endgroup$
– akozi
yesterday
1
$begingroup$
Related (I think).
$endgroup$
– Charlie
15 hours ago
|
show 4 more comments
2
$begingroup$
I think it would be a good idea to add a test case where using the teleporter would make it take longer.
$endgroup$
– Okx
yesterday
$begingroup$
@Okx Creating and adding now.
$endgroup$
– akozi
yesterday
$begingroup$
Edited, thanks.
$endgroup$
– akozi
yesterday
1
$begingroup$
@xnor I feel that this may be to abstract from my original rules. So perhaps it's better to portals a one use item?
$endgroup$
– akozi
yesterday
1
$begingroup$
Related (I think).
$endgroup$
– Charlie
15 hours ago
2
2
$begingroup$
I think it would be a good idea to add a test case where using the teleporter would make it take longer.
$endgroup$
– Okx
yesterday
$begingroup$
I think it would be a good idea to add a test case where using the teleporter would make it take longer.
$endgroup$
– Okx
yesterday
$begingroup$
@Okx Creating and adding now.
$endgroup$
– akozi
yesterday
$begingroup$
@Okx Creating and adding now.
$endgroup$
– akozi
yesterday
$begingroup$
Edited, thanks.
$endgroup$
– akozi
yesterday
$begingroup$
Edited, thanks.
$endgroup$
– akozi
yesterday
1
1
$begingroup$
@xnor I feel that this may be to abstract from my original rules. So perhaps it's better to portals a one use item?
$endgroup$
– akozi
yesterday
$begingroup$
@xnor I feel that this may be to abstract from my original rules. So perhaps it's better to portals a one use item?
$endgroup$
– akozi
yesterday
1
1
$begingroup$
Related (I think).
$endgroup$
– Charlie
15 hours ago
$begingroup$
Related (I think).
$endgroup$
– Charlie
15 hours ago
|
show 4 more comments
3 Answers
3
active
oldest
votes
$begingroup$
JavaScript (ES7), 140 139 138 bytes
Takes input as a matrix of integers with the following mapping:
$-1$ = 🔵 (any portal)
$0$ = $X$ (empty)
$1$ = 🌄 (mountain)
$2$ = 🐢 (turtle)
$3$ = 🍓 (strawberry)
m=>(R=g=(t,X,Y,i)=>m.map((r,y)=>r.map((v,x)=>r[(u=0,t?v-t:(x-X)**2+(y-Y)**2<3?v-3?~v?v:u--:R=R<i?R:i:1)||g(u,x,y,u-~i,r[x]=1),x]=v)))(2)|R
Try it online!
How?
The main recursive search function $g$ is able to either look for a specific tile $t$ on the board (if it's called with $tne 0$) or for any tile at $(x,y)$ which can be reached from the current position $(X,Y)$.
It keeps track of the length of the current path in $i$ and updates the best result $R$ to $min(R,i)$ whenever the turtle finds the strawberry.
It is first called with $t=2$ to find the starting position of the turtle.
It calls itself with $t=-1$ if a portal is reached, so that the turtle is teleported to the other portal. We do not increment $i$ during such an iteration.
Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile in the same path. If we get trapped in a dead-end, the recursion simply stops without updating $R$.
Commented
m => ( // m = input matrix
R = // initialize R to a non-numeric value
g = (t, X, Y, i) => // g = recursive search function taking t = expected tile,
// (X, Y) = current coordinates, i = path length
m.map((r, y) => // for each row r at position y in m:
r.map((v, x) => // for each tile v at position x in r:
r[ // this statement will eventually restore r[x] to v
( u = 0, // u = next tile to look for, or 0 if none
t ? // if we're looking for a specific tile:
v - t // test whether we've found it
: // else:
(x - X) ** 2 + // compute the squared Euclidean distance between
(y - Y) ** 2 // (x, y) and (X, Y)
< 3 ? // if it's less than 3 (i.e. reachable from (X, Y)):
v - 3 ? // if v is not equal to 3:
~v ? // if v is not equal to -1:
v // test if v = 0
: // else (v = -1):
u-- // set u = -1 to find the other portal
: // else (v = 3):
R = R < i ? // we've found the strawberry: set R = min(R, i)
R : i //
: // else (this tile can't be reached):
1 // yield 1
) || // if the above result is falsy:
g( // do a recursive call:
u, // t = u
x, y, // move to (x, y)
u - ~i, // unless u is set to -1, increment i
r[x] = 1 // set this tile to a mountain
), // end of recursive call
x // restore r[x] ...
] = v // ... to v
)) // end of both map() loops
)(2) | R // initial call to g with t = 2; return R
$endgroup$
1
$begingroup$
"Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile" What a lovely trick. Great answer, and as always I appreciate answers with explanations :)
$endgroup$
– akozi
23 hours ago
add a comment |
$begingroup$
Python 2, 441 431 bytes
from itertools import*
G=input()
W=len(G[0])
H=len(G)
for y in range(H):
for x in range(W):
C=G[y][x]
if C==0:t=x,y
if C==1:b=x,y
if C==2:d=x,y
if C==3:s=x,y
P='U D L R UL UR DL DR'.split()
E=0
for L in count():
if E:break
for M in product(*[P]*L):
x,y=t
for m in M:
if'U'in m:y-=1
if'D'in m:y+=1
if'R'in m:x+=1
if'L'in m:x-=1
if(x,y)==s:x,y=d
if 1-(W>x>=0<=y<H)or G[y][x]==4:break
if(x,y)==b:E=1;print L;break
Try it online!
Input as lists, but using numbers instead of characters (thanks to Quintec) and a seperate value for the destination of the teleporter. Those large indentations should be tab characters if Stack Exchange removes them. Any tips or ideas especially welcome, as I feel that this could get much shorter.
The table for characters used in the challenge to the numbers used for my program is below, but you can also use this program.
Challenge | My program
T | 0
S | 1
E | 2
O | 3
M | 4
X | -1
-10 bytes thanks to Quintec by changing the input from using characters to numbers.
$endgroup$
2
$begingroup$
You can probably shave a few characters off by taking in a list where each object is represented by a number instead of a string character.
$endgroup$
– Quintec
yesterday
$begingroup$
@Quintec Added, thanks. I would like to do the same for the directions, but then the diagonals would have to be done separately. It still may be possible to move them to numbers though.
$endgroup$
– nedla2004
yesterday
$begingroup$
@JonathanFrech Sorry, I had changed the TIO to use Quintec's idea, but I had not updated the post. With their idea, yours can no longer be used. I should have updated the code in the post earlier.
$endgroup$
– nedla2004
yesterday
$begingroup$
@nedla2004 No problem. You can now probably golfif'U'in m:y-=1toy-='U'in m, continuing analogously for the other lines.
$endgroup$
– Jonathan Frech
yesterday
$begingroup$
You can useexit(L)to print the result as a return code so losing a couple ofbreaks and the E variable for 409
$endgroup$
– ElPedro
16 hours ago
|
show 7 more comments
$begingroup$
Python 2, 391 397 403 422 bytes
M=input()
from networkx import*
a=b=c=d=0
N,h,w,S=[-1,0,1],len(M),len(M[0]),
for i in range(h):
for j in range(w):
I,m=(i,j),M[i][j]
if m>7:c,d=a,b;a,b=I
if m<0:Z=I
if m==5:F=I
S+=[I+I]
S+=[(a,b,c,d),(c,d,a,b)]
print len(shortest_path(from_edgelist([((A+p,B+q),(C,D))for A,B,C,D in S for p,q in[(p,q)for p in N for q in N]if-1<A+p<h and-1<B+q<w and M[C][D]*M[A+p][B+q]]),Z,F))-1
Try it online!
The problem is translated into a graph and the solution is to find the shortest path form the turtle to the strawberry.
Challenge | This code
T | -1
S | 5
O | 8
M | 0
X | 1
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179441%2fa-turtle-finds-a-portal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
JavaScript (ES7), 140 139 138 bytes
Takes input as a matrix of integers with the following mapping:
$-1$ = 🔵 (any portal)
$0$ = $X$ (empty)
$1$ = 🌄 (mountain)
$2$ = 🐢 (turtle)
$3$ = 🍓 (strawberry)
m=>(R=g=(t,X,Y,i)=>m.map((r,y)=>r.map((v,x)=>r[(u=0,t?v-t:(x-X)**2+(y-Y)**2<3?v-3?~v?v:u--:R=R<i?R:i:1)||g(u,x,y,u-~i,r[x]=1),x]=v)))(2)|R
Try it online!
How?
The main recursive search function $g$ is able to either look for a specific tile $t$ on the board (if it's called with $tne 0$) or for any tile at $(x,y)$ which can be reached from the current position $(X,Y)$.
It keeps track of the length of the current path in $i$ and updates the best result $R$ to $min(R,i)$ whenever the turtle finds the strawberry.
It is first called with $t=2$ to find the starting position of the turtle.
It calls itself with $t=-1$ if a portal is reached, so that the turtle is teleported to the other portal. We do not increment $i$ during such an iteration.
Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile in the same path. If we get trapped in a dead-end, the recursion simply stops without updating $R$.
Commented
m => ( // m = input matrix
R = // initialize R to a non-numeric value
g = (t, X, Y, i) => // g = recursive search function taking t = expected tile,
// (X, Y) = current coordinates, i = path length
m.map((r, y) => // for each row r at position y in m:
r.map((v, x) => // for each tile v at position x in r:
r[ // this statement will eventually restore r[x] to v
( u = 0, // u = next tile to look for, or 0 if none
t ? // if we're looking for a specific tile:
v - t // test whether we've found it
: // else:
(x - X) ** 2 + // compute the squared Euclidean distance between
(y - Y) ** 2 // (x, y) and (X, Y)
< 3 ? // if it's less than 3 (i.e. reachable from (X, Y)):
v - 3 ? // if v is not equal to 3:
~v ? // if v is not equal to -1:
v // test if v = 0
: // else (v = -1):
u-- // set u = -1 to find the other portal
: // else (v = 3):
R = R < i ? // we've found the strawberry: set R = min(R, i)
R : i //
: // else (this tile can't be reached):
1 // yield 1
) || // if the above result is falsy:
g( // do a recursive call:
u, // t = u
x, y, // move to (x, y)
u - ~i, // unless u is set to -1, increment i
r[x] = 1 // set this tile to a mountain
), // end of recursive call
x // restore r[x] ...
] = v // ... to v
)) // end of both map() loops
)(2) | R // initial call to g with t = 2; return R
$endgroup$
1
$begingroup$
"Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile" What a lovely trick. Great answer, and as always I appreciate answers with explanations :)
$endgroup$
– akozi
23 hours ago
add a comment |
$begingroup$
JavaScript (ES7), 140 139 138 bytes
Takes input as a matrix of integers with the following mapping:
$-1$ = 🔵 (any portal)
$0$ = $X$ (empty)
$1$ = 🌄 (mountain)
$2$ = 🐢 (turtle)
$3$ = 🍓 (strawberry)
m=>(R=g=(t,X,Y,i)=>m.map((r,y)=>r.map((v,x)=>r[(u=0,t?v-t:(x-X)**2+(y-Y)**2<3?v-3?~v?v:u--:R=R<i?R:i:1)||g(u,x,y,u-~i,r[x]=1),x]=v)))(2)|R
Try it online!
How?
The main recursive search function $g$ is able to either look for a specific tile $t$ on the board (if it's called with $tne 0$) or for any tile at $(x,y)$ which can be reached from the current position $(X,Y)$.
It keeps track of the length of the current path in $i$ and updates the best result $R$ to $min(R,i)$ whenever the turtle finds the strawberry.
It is first called with $t=2$ to find the starting position of the turtle.
It calls itself with $t=-1$ if a portal is reached, so that the turtle is teleported to the other portal. We do not increment $i$ during such an iteration.
Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile in the same path. If we get trapped in a dead-end, the recursion simply stops without updating $R$.
Commented
m => ( // m = input matrix
R = // initialize R to a non-numeric value
g = (t, X, Y, i) => // g = recursive search function taking t = expected tile,
// (X, Y) = current coordinates, i = path length
m.map((r, y) => // for each row r at position y in m:
r.map((v, x) => // for each tile v at position x in r:
r[ // this statement will eventually restore r[x] to v
( u = 0, // u = next tile to look for, or 0 if none
t ? // if we're looking for a specific tile:
v - t // test whether we've found it
: // else:
(x - X) ** 2 + // compute the squared Euclidean distance between
(y - Y) ** 2 // (x, y) and (X, Y)
< 3 ? // if it's less than 3 (i.e. reachable from (X, Y)):
v - 3 ? // if v is not equal to 3:
~v ? // if v is not equal to -1:
v // test if v = 0
: // else (v = -1):
u-- // set u = -1 to find the other portal
: // else (v = 3):
R = R < i ? // we've found the strawberry: set R = min(R, i)
R : i //
: // else (this tile can't be reached):
1 // yield 1
) || // if the above result is falsy:
g( // do a recursive call:
u, // t = u
x, y, // move to (x, y)
u - ~i, // unless u is set to -1, increment i
r[x] = 1 // set this tile to a mountain
), // end of recursive call
x // restore r[x] ...
] = v // ... to v
)) // end of both map() loops
)(2) | R // initial call to g with t = 2; return R
$endgroup$
1
$begingroup$
"Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile" What a lovely trick. Great answer, and as always I appreciate answers with explanations :)
$endgroup$
– akozi
23 hours ago
add a comment |
$begingroup$
JavaScript (ES7), 140 139 138 bytes
Takes input as a matrix of integers with the following mapping:
$-1$ = 🔵 (any portal)
$0$ = $X$ (empty)
$1$ = 🌄 (mountain)
$2$ = 🐢 (turtle)
$3$ = 🍓 (strawberry)
m=>(R=g=(t,X,Y,i)=>m.map((r,y)=>r.map((v,x)=>r[(u=0,t?v-t:(x-X)**2+(y-Y)**2<3?v-3?~v?v:u--:R=R<i?R:i:1)||g(u,x,y,u-~i,r[x]=1),x]=v)))(2)|R
Try it online!
How?
The main recursive search function $g$ is able to either look for a specific tile $t$ on the board (if it's called with $tne 0$) or for any tile at $(x,y)$ which can be reached from the current position $(X,Y)$.
It keeps track of the length of the current path in $i$ and updates the best result $R$ to $min(R,i)$ whenever the turtle finds the strawberry.
It is first called with $t=2$ to find the starting position of the turtle.
It calls itself with $t=-1$ if a portal is reached, so that the turtle is teleported to the other portal. We do not increment $i$ during such an iteration.
Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile in the same path. If we get trapped in a dead-end, the recursion simply stops without updating $R$.
Commented
m => ( // m = input matrix
R = // initialize R to a non-numeric value
g = (t, X, Y, i) => // g = recursive search function taking t = expected tile,
// (X, Y) = current coordinates, i = path length
m.map((r, y) => // for each row r at position y in m:
r.map((v, x) => // for each tile v at position x in r:
r[ // this statement will eventually restore r[x] to v
( u = 0, // u = next tile to look for, or 0 if none
t ? // if we're looking for a specific tile:
v - t // test whether we've found it
: // else:
(x - X) ** 2 + // compute the squared Euclidean distance between
(y - Y) ** 2 // (x, y) and (X, Y)
< 3 ? // if it's less than 3 (i.e. reachable from (X, Y)):
v - 3 ? // if v is not equal to 3:
~v ? // if v is not equal to -1:
v // test if v = 0
: // else (v = -1):
u-- // set u = -1 to find the other portal
: // else (v = 3):
R = R < i ? // we've found the strawberry: set R = min(R, i)
R : i //
: // else (this tile can't be reached):
1 // yield 1
) || // if the above result is falsy:
g( // do a recursive call:
u, // t = u
x, y, // move to (x, y)
u - ~i, // unless u is set to -1, increment i
r[x] = 1 // set this tile to a mountain
), // end of recursive call
x // restore r[x] ...
] = v // ... to v
)) // end of both map() loops
)(2) | R // initial call to g with t = 2; return R
$endgroup$
JavaScript (ES7), 140 139 138 bytes
Takes input as a matrix of integers with the following mapping:
$-1$ = 🔵 (any portal)
$0$ = $X$ (empty)
$1$ = 🌄 (mountain)
$2$ = 🐢 (turtle)
$3$ = 🍓 (strawberry)
m=>(R=g=(t,X,Y,i)=>m.map((r,y)=>r.map((v,x)=>r[(u=0,t?v-t:(x-X)**2+(y-Y)**2<3?v-3?~v?v:u--:R=R<i?R:i:1)||g(u,x,y,u-~i,r[x]=1),x]=v)))(2)|R
Try it online!
How?
The main recursive search function $g$ is able to either look for a specific tile $t$ on the board (if it's called with $tne 0$) or for any tile at $(x,y)$ which can be reached from the current position $(X,Y)$.
It keeps track of the length of the current path in $i$ and updates the best result $R$ to $min(R,i)$ whenever the turtle finds the strawberry.
It is first called with $t=2$ to find the starting position of the turtle.
It calls itself with $t=-1$ if a portal is reached, so that the turtle is teleported to the other portal. We do not increment $i$ during such an iteration.
Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile in the same path. If we get trapped in a dead-end, the recursion simply stops without updating $R$.
Commented
m => ( // m = input matrix
R = // initialize R to a non-numeric value
g = (t, X, Y, i) => // g = recursive search function taking t = expected tile,
// (X, Y) = current coordinates, i = path length
m.map((r, y) => // for each row r at position y in m:
r.map((v, x) => // for each tile v at position x in r:
r[ // this statement will eventually restore r[x] to v
( u = 0, // u = next tile to look for, or 0 if none
t ? // if we're looking for a specific tile:
v - t // test whether we've found it
: // else:
(x - X) ** 2 + // compute the squared Euclidean distance between
(y - Y) ** 2 // (x, y) and (X, Y)
< 3 ? // if it's less than 3 (i.e. reachable from (X, Y)):
v - 3 ? // if v is not equal to 3:
~v ? // if v is not equal to -1:
v // test if v = 0
: // else (v = -1):
u-- // set u = -1 to find the other portal
: // else (v = 3):
R = R < i ? // we've found the strawberry: set R = min(R, i)
R : i //
: // else (this tile can't be reached):
1 // yield 1
) || // if the above result is falsy:
g( // do a recursive call:
u, // t = u
x, y, // move to (x, y)
u - ~i, // unless u is set to -1, increment i
r[x] = 1 // set this tile to a mountain
), // end of recursive call
x // restore r[x] ...
] = v // ... to v
)) // end of both map() loops
)(2) | R // initial call to g with t = 2; return R
edited yesterday
answered yesterday
ArnauldArnauld
75.3k691317
75.3k691317
1
$begingroup$
"Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile" What a lovely trick. Great answer, and as always I appreciate answers with explanations :)
$endgroup$
– akozi
23 hours ago
add a comment |
1
$begingroup$
"Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile" What a lovely trick. Great answer, and as always I appreciate answers with explanations :)
$endgroup$
– akozi
23 hours ago
1
1
$begingroup$
"Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile" What a lovely trick. Great answer, and as always I appreciate answers with explanations :)
$endgroup$
– akozi
23 hours ago
$begingroup$
"Each visited tile is temporarily set to a mountain to prevent the turtle from moving twice on the same tile" What a lovely trick. Great answer, and as always I appreciate answers with explanations :)
$endgroup$
– akozi
23 hours ago
add a comment |
$begingroup$
Python 2, 441 431 bytes
from itertools import*
G=input()
W=len(G[0])
H=len(G)
for y in range(H):
for x in range(W):
C=G[y][x]
if C==0:t=x,y
if C==1:b=x,y
if C==2:d=x,y
if C==3:s=x,y
P='U D L R UL UR DL DR'.split()
E=0
for L in count():
if E:break
for M in product(*[P]*L):
x,y=t
for m in M:
if'U'in m:y-=1
if'D'in m:y+=1
if'R'in m:x+=1
if'L'in m:x-=1
if(x,y)==s:x,y=d
if 1-(W>x>=0<=y<H)or G[y][x]==4:break
if(x,y)==b:E=1;print L;break
Try it online!
Input as lists, but using numbers instead of characters (thanks to Quintec) and a seperate value for the destination of the teleporter. Those large indentations should be tab characters if Stack Exchange removes them. Any tips or ideas especially welcome, as I feel that this could get much shorter.
The table for characters used in the challenge to the numbers used for my program is below, but you can also use this program.
Challenge | My program
T | 0
S | 1
E | 2
O | 3
M | 4
X | -1
-10 bytes thanks to Quintec by changing the input from using characters to numbers.
$endgroup$
2
$begingroup$
You can probably shave a few characters off by taking in a list where each object is represented by a number instead of a string character.
$endgroup$
– Quintec
yesterday
$begingroup$
@Quintec Added, thanks. I would like to do the same for the directions, but then the diagonals would have to be done separately. It still may be possible to move them to numbers though.
$endgroup$
– nedla2004
yesterday
$begingroup$
@JonathanFrech Sorry, I had changed the TIO to use Quintec's idea, but I had not updated the post. With their idea, yours can no longer be used. I should have updated the code in the post earlier.
$endgroup$
– nedla2004
yesterday
$begingroup$
@nedla2004 No problem. You can now probably golfif'U'in m:y-=1toy-='U'in m, continuing analogously for the other lines.
$endgroup$
– Jonathan Frech
yesterday
$begingroup$
You can useexit(L)to print the result as a return code so losing a couple ofbreaks and the E variable for 409
$endgroup$
– ElPedro
16 hours ago
|
show 7 more comments
$begingroup$
Python 2, 441 431 bytes
from itertools import*
G=input()
W=len(G[0])
H=len(G)
for y in range(H):
for x in range(W):
C=G[y][x]
if C==0:t=x,y
if C==1:b=x,y
if C==2:d=x,y
if C==3:s=x,y
P='U D L R UL UR DL DR'.split()
E=0
for L in count():
if E:break
for M in product(*[P]*L):
x,y=t
for m in M:
if'U'in m:y-=1
if'D'in m:y+=1
if'R'in m:x+=1
if'L'in m:x-=1
if(x,y)==s:x,y=d
if 1-(W>x>=0<=y<H)or G[y][x]==4:break
if(x,y)==b:E=1;print L;break
Try it online!
Input as lists, but using numbers instead of characters (thanks to Quintec) and a seperate value for the destination of the teleporter. Those large indentations should be tab characters if Stack Exchange removes them. Any tips or ideas especially welcome, as I feel that this could get much shorter.
The table for characters used in the challenge to the numbers used for my program is below, but you can also use this program.
Challenge | My program
T | 0
S | 1
E | 2
O | 3
M | 4
X | -1
-10 bytes thanks to Quintec by changing the input from using characters to numbers.
$endgroup$
2
$begingroup$
You can probably shave a few characters off by taking in a list where each object is represented by a number instead of a string character.
$endgroup$
– Quintec
yesterday
$begingroup$
@Quintec Added, thanks. I would like to do the same for the directions, but then the diagonals would have to be done separately. It still may be possible to move them to numbers though.
$endgroup$
– nedla2004
yesterday
$begingroup$
@JonathanFrech Sorry, I had changed the TIO to use Quintec's idea, but I had not updated the post. With their idea, yours can no longer be used. I should have updated the code in the post earlier.
$endgroup$
– nedla2004
yesterday
$begingroup$
@nedla2004 No problem. You can now probably golfif'U'in m:y-=1toy-='U'in m, continuing analogously for the other lines.
$endgroup$
– Jonathan Frech
yesterday
$begingroup$
You can useexit(L)to print the result as a return code so losing a couple ofbreaks and the E variable for 409
$endgroup$
– ElPedro
16 hours ago
|
show 7 more comments
$begingroup$
Python 2, 441 431 bytes
from itertools import*
G=input()
W=len(G[0])
H=len(G)
for y in range(H):
for x in range(W):
C=G[y][x]
if C==0:t=x,y
if C==1:b=x,y
if C==2:d=x,y
if C==3:s=x,y
P='U D L R UL UR DL DR'.split()
E=0
for L in count():
if E:break
for M in product(*[P]*L):
x,y=t
for m in M:
if'U'in m:y-=1
if'D'in m:y+=1
if'R'in m:x+=1
if'L'in m:x-=1
if(x,y)==s:x,y=d
if 1-(W>x>=0<=y<H)or G[y][x]==4:break
if(x,y)==b:E=1;print L;break
Try it online!
Input as lists, but using numbers instead of characters (thanks to Quintec) and a seperate value for the destination of the teleporter. Those large indentations should be tab characters if Stack Exchange removes them. Any tips or ideas especially welcome, as I feel that this could get much shorter.
The table for characters used in the challenge to the numbers used for my program is below, but you can also use this program.
Challenge | My program
T | 0
S | 1
E | 2
O | 3
M | 4
X | -1
-10 bytes thanks to Quintec by changing the input from using characters to numbers.
$endgroup$
Python 2, 441 431 bytes
from itertools import*
G=input()
W=len(G[0])
H=len(G)
for y in range(H):
for x in range(W):
C=G[y][x]
if C==0:t=x,y
if C==1:b=x,y
if C==2:d=x,y
if C==3:s=x,y
P='U D L R UL UR DL DR'.split()
E=0
for L in count():
if E:break
for M in product(*[P]*L):
x,y=t
for m in M:
if'U'in m:y-=1
if'D'in m:y+=1
if'R'in m:x+=1
if'L'in m:x-=1
if(x,y)==s:x,y=d
if 1-(W>x>=0<=y<H)or G[y][x]==4:break
if(x,y)==b:E=1;print L;break
Try it online!
Input as lists, but using numbers instead of characters (thanks to Quintec) and a seperate value for the destination of the teleporter. Those large indentations should be tab characters if Stack Exchange removes them. Any tips or ideas especially welcome, as I feel that this could get much shorter.
The table for characters used in the challenge to the numbers used for my program is below, but you can also use this program.
Challenge | My program
T | 0
S | 1
E | 2
O | 3
M | 4
X | -1
-10 bytes thanks to Quintec by changing the input from using characters to numbers.
edited yesterday
answered yesterday
nedla2004nedla2004
4711410
4711410
2
$begingroup$
You can probably shave a few characters off by taking in a list where each object is represented by a number instead of a string character.
$endgroup$
– Quintec
yesterday
$begingroup$
@Quintec Added, thanks. I would like to do the same for the directions, but then the diagonals would have to be done separately. It still may be possible to move them to numbers though.
$endgroup$
– nedla2004
yesterday
$begingroup$
@JonathanFrech Sorry, I had changed the TIO to use Quintec's idea, but I had not updated the post. With their idea, yours can no longer be used. I should have updated the code in the post earlier.
$endgroup$
– nedla2004
yesterday
$begingroup$
@nedla2004 No problem. You can now probably golfif'U'in m:y-=1toy-='U'in m, continuing analogously for the other lines.
$endgroup$
– Jonathan Frech
yesterday
$begingroup$
You can useexit(L)to print the result as a return code so losing a couple ofbreaks and the E variable for 409
$endgroup$
– ElPedro
16 hours ago
|
show 7 more comments
2
$begingroup$
You can probably shave a few characters off by taking in a list where each object is represented by a number instead of a string character.
$endgroup$
– Quintec
yesterday
$begingroup$
@Quintec Added, thanks. I would like to do the same for the directions, but then the diagonals would have to be done separately. It still may be possible to move them to numbers though.
$endgroup$
– nedla2004
yesterday
$begingroup$
@JonathanFrech Sorry, I had changed the TIO to use Quintec's idea, but I had not updated the post. With their idea, yours can no longer be used. I should have updated the code in the post earlier.
$endgroup$
– nedla2004
yesterday
$begingroup$
@nedla2004 No problem. You can now probably golfif'U'in m:y-=1toy-='U'in m, continuing analogously for the other lines.
$endgroup$
– Jonathan Frech
yesterday
$begingroup$
You can useexit(L)to print the result as a return code so losing a couple ofbreaks and the E variable for 409
$endgroup$
– ElPedro
16 hours ago
2
2
$begingroup$
You can probably shave a few characters off by taking in a list where each object is represented by a number instead of a string character.
$endgroup$
– Quintec
yesterday
$begingroup$
You can probably shave a few characters off by taking in a list where each object is represented by a number instead of a string character.
$endgroup$
– Quintec
yesterday
$begingroup$
@Quintec Added, thanks. I would like to do the same for the directions, but then the diagonals would have to be done separately. It still may be possible to move them to numbers though.
$endgroup$
– nedla2004
yesterday
$begingroup$
@Quintec Added, thanks. I would like to do the same for the directions, but then the diagonals would have to be done separately. It still may be possible to move them to numbers though.
$endgroup$
– nedla2004
yesterday
$begingroup$
@JonathanFrech Sorry, I had changed the TIO to use Quintec's idea, but I had not updated the post. With their idea, yours can no longer be used. I should have updated the code in the post earlier.
$endgroup$
– nedla2004
yesterday
$begingroup$
@JonathanFrech Sorry, I had changed the TIO to use Quintec's idea, but I had not updated the post. With their idea, yours can no longer be used. I should have updated the code in the post earlier.
$endgroup$
– nedla2004
yesterday
$begingroup$
@nedla2004 No problem. You can now probably golf
if'U'in m:y-=1 to y-='U'in m, continuing analogously for the other lines.$endgroup$
– Jonathan Frech
yesterday
$begingroup$
@nedla2004 No problem. You can now probably golf
if'U'in m:y-=1 to y-='U'in m, continuing analogously for the other lines.$endgroup$
– Jonathan Frech
yesterday
$begingroup$
You can use
exit(L) to print the result as a return code so losing a couple of breaks and the E variable for 409$endgroup$
– ElPedro
16 hours ago
$begingroup$
You can use
exit(L) to print the result as a return code so losing a couple of breaks and the E variable for 409$endgroup$
– ElPedro
16 hours ago
|
show 7 more comments
$begingroup$
Python 2, 391 397 403 422 bytes
M=input()
from networkx import*
a=b=c=d=0
N,h,w,S=[-1,0,1],len(M),len(M[0]),
for i in range(h):
for j in range(w):
I,m=(i,j),M[i][j]
if m>7:c,d=a,b;a,b=I
if m<0:Z=I
if m==5:F=I
S+=[I+I]
S+=[(a,b,c,d),(c,d,a,b)]
print len(shortest_path(from_edgelist([((A+p,B+q),(C,D))for A,B,C,D in S for p,q in[(p,q)for p in N for q in N]if-1<A+p<h and-1<B+q<w and M[C][D]*M[A+p][B+q]]),Z,F))-1
Try it online!
The problem is translated into a graph and the solution is to find the shortest path form the turtle to the strawberry.
Challenge | This code
T | -1
S | 5
O | 8
M | 0
X | 1
$endgroup$
add a comment |
$begingroup$
Python 2, 391 397 403 422 bytes
M=input()
from networkx import*
a=b=c=d=0
N,h,w,S=[-1,0,1],len(M),len(M[0]),
for i in range(h):
for j in range(w):
I,m=(i,j),M[i][j]
if m>7:c,d=a,b;a,b=I
if m<0:Z=I
if m==5:F=I
S+=[I+I]
S+=[(a,b,c,d),(c,d,a,b)]
print len(shortest_path(from_edgelist([((A+p,B+q),(C,D))for A,B,C,D in S for p,q in[(p,q)for p in N for q in N]if-1<A+p<h and-1<B+q<w and M[C][D]*M[A+p][B+q]]),Z,F))-1
Try it online!
The problem is translated into a graph and the solution is to find the shortest path form the turtle to the strawberry.
Challenge | This code
T | -1
S | 5
O | 8
M | 0
X | 1
$endgroup$
add a comment |
$begingroup$
Python 2, 391 397 403 422 bytes
M=input()
from networkx import*
a=b=c=d=0
N,h,w,S=[-1,0,1],len(M),len(M[0]),
for i in range(h):
for j in range(w):
I,m=(i,j),M[i][j]
if m>7:c,d=a,b;a,b=I
if m<0:Z=I
if m==5:F=I
S+=[I+I]
S+=[(a,b,c,d),(c,d,a,b)]
print len(shortest_path(from_edgelist([((A+p,B+q),(C,D))for A,B,C,D in S for p,q in[(p,q)for p in N for q in N]if-1<A+p<h and-1<B+q<w and M[C][D]*M[A+p][B+q]]),Z,F))-1
Try it online!
The problem is translated into a graph and the solution is to find the shortest path form the turtle to the strawberry.
Challenge | This code
T | -1
S | 5
O | 8
M | 0
X | 1
$endgroup$
Python 2, 391 397 403 422 bytes
M=input()
from networkx import*
a=b=c=d=0
N,h,w,S=[-1,0,1],len(M),len(M[0]),
for i in range(h):
for j in range(w):
I,m=(i,j),M[i][j]
if m>7:c,d=a,b;a,b=I
if m<0:Z=I
if m==5:F=I
S+=[I+I]
S+=[(a,b,c,d),(c,d,a,b)]
print len(shortest_path(from_edgelist([((A+p,B+q),(C,D))for A,B,C,D in S for p,q in[(p,q)for p in N for q in N]if-1<A+p<h and-1<B+q<w and M[C][D]*M[A+p][B+q]]),Z,F))-1
Try it online!
The problem is translated into a graph and the solution is to find the shortest path form the turtle to the strawberry.
Challenge | This code
T | -1
S | 5
O | 8
M | 0
X | 1
edited 8 hours ago
answered 9 hours ago
mdahmounemdahmoune
1,6151723
1,6151723
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f179441%2fa-turtle-finds-a-portal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
$begingroup$
I think it would be a good idea to add a test case where using the teleporter would make it take longer.
$endgroup$
– Okx
yesterday
$begingroup$
@Okx Creating and adding now.
$endgroup$
– akozi
yesterday
$begingroup$
Edited, thanks.
$endgroup$
– akozi
yesterday
1
$begingroup$
@xnor I feel that this may be to abstract from my original rules. So perhaps it's better to portals a one use item?
$endgroup$
– akozi
yesterday
1
$begingroup$
Related (I think).
$endgroup$
– Charlie
15 hours ago