Fill the maze with a wall-following Snake until it gets stuckRecreate a 'Snake' game in a console/terminalCalculate the probability of seeing a landmark when starting at a given point and walking straight in a random directionSnakes all aroundA Mouse with DynamitepssssssssssssstKnight-fill a gridA Room Of MirrorsRender an ASCII mazeDungeon CrawlerASCII Maze Compression
Boss making me feel guilty for leaving the company at the end of my internship
How to ask if I can mow my neighbor's lawn
Testing thermite for chemical properties
How do I run a script as sudo at boot time on Ubuntu 18.04 Server?
How to address players struggling with simple controls?
What is the precise meaning of "подсел на мак"?
How can I maintain game balance while allowing my player to craft genuinely useful items?
Why does my system use more RAM after an hour of usage?
First occurrence in the Sixers sequence
what is "dot" sign in the •NO?
Why are almost all the people in this orchestra recording wearing headphones with one ear on and one ear off?
Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?
Leveraging cash for buying car
100-doors puzzle
Can you cover a cube with copies of this shape?
Can I drive in EU states and Switzerland with German proof of a surrendered U.S. license?
Is the infant mortality rate among African-American babies in Youngstown, Ohio greater than that of babies in Iran?
At what temperature should the earth be cooked to prevent human infection?
How did space travel spread through the galaxy?
How do credit card companies know what type of business I'm paying for?
Is swap gate equivalent to just exchanging the wire of the two qubits?
Leaving job close to major deadlines
Numerical second order differentiation
Does anyone recognize these rockets, and their location?
Fill the maze with a wall-following Snake until it gets stuck
Recreate a 'Snake' game in a console/terminalCalculate the probability of seeing a landmark when starting at a given point and walking straight in a random directionSnakes all aroundA Mouse with DynamitepssssssssssssstKnight-fill a gridA Room Of MirrorsRender an ASCII mazeDungeon CrawlerASCII Maze Compression
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Make a snake fill any maze (until it gets stuck).
The snake
The snake starts at a given starting point, pointing EAST. It moves by always having a wall or a part of its body immediately to the LEFT of its head ("left-hand rule wall follower"), until it gets stuck because all four directions around its head are occupied.
(Note: a stuck snake can possibly not fill all the reachable space, but that is not the goal!)
The challenge
Write a program or function that accepts a maze as input in the form of a 2D text:
- The input can be in any reasonable format: e.g. a list of strings, a single string with newlines, a file.
- The maze has walls ("
#"), empty spaces ("") and exactly one starting point ("o"). You can choose to
- either assume that the first and last row and column are entirely walls;
- or assume that every input is considered to have an implicit outer layer of walls
You can assume that the starting point has a wall (or an implicit wall) directly above it (NORTH) and that the snake can make a valid starting move in the EAST or SOUTH direction.
- You can assume that no other characters are present in the text (except newlines if your input needs them).
- You can assume that all lines are the same length.
and prints / returns a "filled maze" as output, with a snapshot of the snake at the moment it got stuck:
- The body of the snake is represented by the characters
>v<^pointing to where its next segment is - The starting point of the snake is either its direction at the start ("
>" unless it had to turn immediately) or anocharacter (no need to be consistent) - The end point of the snake is an
ocharacter
Scoring
Usual code golf: the shortest code wins
Example
in:
#################################
# o #
# #
# ## ### ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ### ## #
# ## ##### ## #
# ## ##### ## #
# ## ### ## #
# ## ## #
# #
# #
#################################
out:
#################################
#>>>>>>>>>>>>>>>>>>>v>>>>>>>>>>v#
#^>>>>>>>>>>>>>>>>>v>>>>>>>>>>vv#
#^^ ##>>>>>>v###o>>>>>v## vv#
#^^ ##>^ ##>>>>^## >v## vv#
#^^ ##^ ## ## v## vv#
#^^ ##^ ## ## v## vv#
#^^ ##>^ ## ## >v## vv#
#^^ ##^< ### v<## vv#
#^^ ##^ ##### v## vv#
#^^ ##^ ##### v## vv#
#^^ ##^< ### v<## vv#
#^^ ##^<<<<<<<<<<<<<<<<## vv#
#^^<<<<<<<<<<<<<<<<<<<<<<<<<<<<v#
#^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<#
#################################
Animated (for illustration purposes):

Other examples
in:
####################
# o# #
# ###
# #
# ## #
# ###
####################
out:
####################
#>>>>>>>>>>>>>>vv# #
#^>>>>>>>>>>>>vvv###
#^^ v<<<o<<<<v>>v#
#^^<<<<##^<<<<<<v<<#
#^<<<<<<<<<<<<<<<###
####################
in:
####################
# o #####
# #####
# #
# ##
####################
out:
####################
# >>>>v#####
# v#####
# >>>>o#
# ##
####################
in:
################
#o #
# ########## #
# # # #
# # # #
# # # #
# # # # #
# # # #
# # # #
# # # #
# ############ #
# #
################
out:
################
#>>>>>>>>>>>>>v#
#>>v##########v#
#^#>>>>>>>>>v#v#
#^#>>>>>>>>vv#v#
#^#^>>>>>>vvv#v#
#^#^^# vvv#v#
#^#^^o<<<<<vv#v#
#^#^^<<<<<<<v#v#
#^#^<<<<<<<<<#v#
#^############v#
#^<<<<<<<<<<<<<#
################
code-golf ascii-art
$endgroup$
|
show 3 more comments
$begingroup$
Make a snake fill any maze (until it gets stuck).
The snake
The snake starts at a given starting point, pointing EAST. It moves by always having a wall or a part of its body immediately to the LEFT of its head ("left-hand rule wall follower"), until it gets stuck because all four directions around its head are occupied.
(Note: a stuck snake can possibly not fill all the reachable space, but that is not the goal!)
The challenge
Write a program or function that accepts a maze as input in the form of a 2D text:
- The input can be in any reasonable format: e.g. a list of strings, a single string with newlines, a file.
- The maze has walls ("
#"), empty spaces ("") and exactly one starting point ("o"). You can choose to
- either assume that the first and last row and column are entirely walls;
- or assume that every input is considered to have an implicit outer layer of walls
You can assume that the starting point has a wall (or an implicit wall) directly above it (NORTH) and that the snake can make a valid starting move in the EAST or SOUTH direction.
- You can assume that no other characters are present in the text (except newlines if your input needs them).
- You can assume that all lines are the same length.
and prints / returns a "filled maze" as output, with a snapshot of the snake at the moment it got stuck:
- The body of the snake is represented by the characters
>v<^pointing to where its next segment is - The starting point of the snake is either its direction at the start ("
>" unless it had to turn immediately) or anocharacter (no need to be consistent) - The end point of the snake is an
ocharacter
Scoring
Usual code golf: the shortest code wins
Example
in:
#################################
# o #
# #
# ## ### ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ### ## #
# ## ##### ## #
# ## ##### ## #
# ## ### ## #
# ## ## #
# #
# #
#################################
out:
#################################
#>>>>>>>>>>>>>>>>>>>v>>>>>>>>>>v#
#^>>>>>>>>>>>>>>>>>v>>>>>>>>>>vv#
#^^ ##>>>>>>v###o>>>>>v## vv#
#^^ ##>^ ##>>>>^## >v## vv#
#^^ ##^ ## ## v## vv#
#^^ ##^ ## ## v## vv#
#^^ ##>^ ## ## >v## vv#
#^^ ##^< ### v<## vv#
#^^ ##^ ##### v## vv#
#^^ ##^ ##### v## vv#
#^^ ##^< ### v<## vv#
#^^ ##^<<<<<<<<<<<<<<<<## vv#
#^^<<<<<<<<<<<<<<<<<<<<<<<<<<<<v#
#^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<#
#################################
Animated (for illustration purposes):

Other examples
in:
####################
# o# #
# ###
# #
# ## #
# ###
####################
out:
####################
#>>>>>>>>>>>>>>vv# #
#^>>>>>>>>>>>>vvv###
#^^ v<<<o<<<<v>>v#
#^^<<<<##^<<<<<<v<<#
#^<<<<<<<<<<<<<<<###
####################
in:
####################
# o #####
# #####
# #
# ##
####################
out:
####################
# >>>>v#####
# v#####
# >>>>o#
# ##
####################
in:
################
#o #
# ########## #
# # # #
# # # #
# # # #
# # # # #
# # # #
# # # #
# # # #
# ############ #
# #
################
out:
################
#>>>>>>>>>>>>>v#
#>>v##########v#
#^#>>>>>>>>>v#v#
#^#>>>>>>>>vv#v#
#^#^>>>>>>vvv#v#
#^#^^# vvv#v#
#^#^^o<<<<<vv#v#
#^#^^<<<<<<<v#v#
#^#^<<<<<<<<<#v#
#^############v#
#^<<<<<<<<<<<<<#
################
code-golf ascii-art
$endgroup$
$begingroup$
In the GIF example, when it enters the pattern why would it not turn back to fill in the other parts of the empty spaces? It was definitely possible to turn left then up then left then down then back around, but the snake chose straight down instead. Is the goal to fill as many spaces as possible with the snake or follow only the "turn right when encountering a wall and end if turn right twice" strategy?
$endgroup$
– Magic Octopus Urn
7 hours ago
$begingroup$
@MagicOctopusUrn I'm not sure I understand at what point you see the ambiguity. But, to answer your question, the goal is not to fill as much space as possible. However, the algorithm ("wall follower") is not just "turn right once or, if not possible, twice": if the snake finds itself without a wall on its left, it will have to turn left instead (keeping the "left hand" on the wall/on itself)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
(sorry I misread your algorithm rundown)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
In the main example the 12th time the head turns (immediately right of the endo) the snake could turn to it's right and still have body to its head's left. Is there an unwritten rule that turning left should be the first option it tries, or are either acceptable at that point? (or have I missed something else?)
$endgroup$
– Jonathan Allan
5 hours ago
$begingroup$
so the amount of fill does not have to be optimal among all possible paths?
$endgroup$
– Jonah
4 hours ago
|
show 3 more comments
$begingroup$
Make a snake fill any maze (until it gets stuck).
The snake
The snake starts at a given starting point, pointing EAST. It moves by always having a wall or a part of its body immediately to the LEFT of its head ("left-hand rule wall follower"), until it gets stuck because all four directions around its head are occupied.
(Note: a stuck snake can possibly not fill all the reachable space, but that is not the goal!)
The challenge
Write a program or function that accepts a maze as input in the form of a 2D text:
- The input can be in any reasonable format: e.g. a list of strings, a single string with newlines, a file.
- The maze has walls ("
#"), empty spaces ("") and exactly one starting point ("o"). You can choose to
- either assume that the first and last row and column are entirely walls;
- or assume that every input is considered to have an implicit outer layer of walls
You can assume that the starting point has a wall (or an implicit wall) directly above it (NORTH) and that the snake can make a valid starting move in the EAST or SOUTH direction.
- You can assume that no other characters are present in the text (except newlines if your input needs them).
- You can assume that all lines are the same length.
and prints / returns a "filled maze" as output, with a snapshot of the snake at the moment it got stuck:
- The body of the snake is represented by the characters
>v<^pointing to where its next segment is - The starting point of the snake is either its direction at the start ("
>" unless it had to turn immediately) or anocharacter (no need to be consistent) - The end point of the snake is an
ocharacter
Scoring
Usual code golf: the shortest code wins
Example
in:
#################################
# o #
# #
# ## ### ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ### ## #
# ## ##### ## #
# ## ##### ## #
# ## ### ## #
# ## ## #
# #
# #
#################################
out:
#################################
#>>>>>>>>>>>>>>>>>>>v>>>>>>>>>>v#
#^>>>>>>>>>>>>>>>>>v>>>>>>>>>>vv#
#^^ ##>>>>>>v###o>>>>>v## vv#
#^^ ##>^ ##>>>>^## >v## vv#
#^^ ##^ ## ## v## vv#
#^^ ##^ ## ## v## vv#
#^^ ##>^ ## ## >v## vv#
#^^ ##^< ### v<## vv#
#^^ ##^ ##### v## vv#
#^^ ##^ ##### v## vv#
#^^ ##^< ### v<## vv#
#^^ ##^<<<<<<<<<<<<<<<<## vv#
#^^<<<<<<<<<<<<<<<<<<<<<<<<<<<<v#
#^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<#
#################################
Animated (for illustration purposes):

Other examples
in:
####################
# o# #
# ###
# #
# ## #
# ###
####################
out:
####################
#>>>>>>>>>>>>>>vv# #
#^>>>>>>>>>>>>vvv###
#^^ v<<<o<<<<v>>v#
#^^<<<<##^<<<<<<v<<#
#^<<<<<<<<<<<<<<<###
####################
in:
####################
# o #####
# #####
# #
# ##
####################
out:
####################
# >>>>v#####
# v#####
# >>>>o#
# ##
####################
in:
################
#o #
# ########## #
# # # #
# # # #
# # # #
# # # # #
# # # #
# # # #
# # # #
# ############ #
# #
################
out:
################
#>>>>>>>>>>>>>v#
#>>v##########v#
#^#>>>>>>>>>v#v#
#^#>>>>>>>>vv#v#
#^#^>>>>>>vvv#v#
#^#^^# vvv#v#
#^#^^o<<<<<vv#v#
#^#^^<<<<<<<v#v#
#^#^<<<<<<<<<#v#
#^############v#
#^<<<<<<<<<<<<<#
################
code-golf ascii-art
$endgroup$
Make a snake fill any maze (until it gets stuck).
The snake
The snake starts at a given starting point, pointing EAST. It moves by always having a wall or a part of its body immediately to the LEFT of its head ("left-hand rule wall follower"), until it gets stuck because all four directions around its head are occupied.
(Note: a stuck snake can possibly not fill all the reachable space, but that is not the goal!)
The challenge
Write a program or function that accepts a maze as input in the form of a 2D text:
- The input can be in any reasonable format: e.g. a list of strings, a single string with newlines, a file.
- The maze has walls ("
#"), empty spaces ("") and exactly one starting point ("o"). You can choose to
- either assume that the first and last row and column are entirely walls;
- or assume that every input is considered to have an implicit outer layer of walls
You can assume that the starting point has a wall (or an implicit wall) directly above it (NORTH) and that the snake can make a valid starting move in the EAST or SOUTH direction.
- You can assume that no other characters are present in the text (except newlines if your input needs them).
- You can assume that all lines are the same length.
and prints / returns a "filled maze" as output, with a snapshot of the snake at the moment it got stuck:
- The body of the snake is represented by the characters
>v<^pointing to where its next segment is - The starting point of the snake is either its direction at the start ("
>" unless it had to turn immediately) or anocharacter (no need to be consistent) - The end point of the snake is an
ocharacter
Scoring
Usual code golf: the shortest code wins
Example
in:
#################################
# o #
# #
# ## ### ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ## ## ## #
# ## ### ## #
# ## ##### ## #
# ## ##### ## #
# ## ### ## #
# ## ## #
# #
# #
#################################
out:
#################################
#>>>>>>>>>>>>>>>>>>>v>>>>>>>>>>v#
#^>>>>>>>>>>>>>>>>>v>>>>>>>>>>vv#
#^^ ##>>>>>>v###o>>>>>v## vv#
#^^ ##>^ ##>>>>^## >v## vv#
#^^ ##^ ## ## v## vv#
#^^ ##^ ## ## v## vv#
#^^ ##>^ ## ## >v## vv#
#^^ ##^< ### v<## vv#
#^^ ##^ ##### v## vv#
#^^ ##^ ##### v## vv#
#^^ ##^< ### v<## vv#
#^^ ##^<<<<<<<<<<<<<<<<## vv#
#^^<<<<<<<<<<<<<<<<<<<<<<<<<<<<v#
#^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<#
#################################
Animated (for illustration purposes):

Other examples
in:
####################
# o# #
# ###
# #
# ## #
# ###
####################
out:
####################
#>>>>>>>>>>>>>>vv# #
#^>>>>>>>>>>>>vvv###
#^^ v<<<o<<<<v>>v#
#^^<<<<##^<<<<<<v<<#
#^<<<<<<<<<<<<<<<###
####################
in:
####################
# o #####
# #####
# #
# ##
####################
out:
####################
# >>>>v#####
# v#####
# >>>>o#
# ##
####################
in:
################
#o #
# ########## #
# # # #
# # # #
# # # #
# # # # #
# # # #
# # # #
# # # #
# ############ #
# #
################
out:
################
#>>>>>>>>>>>>>v#
#>>v##########v#
#^#>>>>>>>>>v#v#
#^#>>>>>>>>vv#v#
#^#^>>>>>>vvv#v#
#^#^^# vvv#v#
#^#^^o<<<<<vv#v#
#^#^^<<<<<<<v#v#
#^#^<<<<<<<<<#v#
#^############v#
#^<<<<<<<<<<<<<#
################
code-golf ascii-art
code-golf ascii-art
edited 4 hours ago
Nicola Sap
asked 8 hours ago
Nicola SapNicola Sap
1,2672411
1,2672411
$begingroup$
In the GIF example, when it enters the pattern why would it not turn back to fill in the other parts of the empty spaces? It was definitely possible to turn left then up then left then down then back around, but the snake chose straight down instead. Is the goal to fill as many spaces as possible with the snake or follow only the "turn right when encountering a wall and end if turn right twice" strategy?
$endgroup$
– Magic Octopus Urn
7 hours ago
$begingroup$
@MagicOctopusUrn I'm not sure I understand at what point you see the ambiguity. But, to answer your question, the goal is not to fill as much space as possible. However, the algorithm ("wall follower") is not just "turn right once or, if not possible, twice": if the snake finds itself without a wall on its left, it will have to turn left instead (keeping the "left hand" on the wall/on itself)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
(sorry I misread your algorithm rundown)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
In the main example the 12th time the head turns (immediately right of the endo) the snake could turn to it's right and still have body to its head's left. Is there an unwritten rule that turning left should be the first option it tries, or are either acceptable at that point? (or have I missed something else?)
$endgroup$
– Jonathan Allan
5 hours ago
$begingroup$
so the amount of fill does not have to be optimal among all possible paths?
$endgroup$
– Jonah
4 hours ago
|
show 3 more comments
$begingroup$
In the GIF example, when it enters the pattern why would it not turn back to fill in the other parts of the empty spaces? It was definitely possible to turn left then up then left then down then back around, but the snake chose straight down instead. Is the goal to fill as many spaces as possible with the snake or follow only the "turn right when encountering a wall and end if turn right twice" strategy?
$endgroup$
– Magic Octopus Urn
7 hours ago
$begingroup$
@MagicOctopusUrn I'm not sure I understand at what point you see the ambiguity. But, to answer your question, the goal is not to fill as much space as possible. However, the algorithm ("wall follower") is not just "turn right once or, if not possible, twice": if the snake finds itself without a wall on its left, it will have to turn left instead (keeping the "left hand" on the wall/on itself)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
(sorry I misread your algorithm rundown)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
In the main example the 12th time the head turns (immediately right of the endo) the snake could turn to it's right and still have body to its head's left. Is there an unwritten rule that turning left should be the first option it tries, or are either acceptable at that point? (or have I missed something else?)
$endgroup$
– Jonathan Allan
5 hours ago
$begingroup$
so the amount of fill does not have to be optimal among all possible paths?
$endgroup$
– Jonah
4 hours ago
$begingroup$
In the GIF example, when it enters the pattern why would it not turn back to fill in the other parts of the empty spaces? It was definitely possible to turn left then up then left then down then back around, but the snake chose straight down instead. Is the goal to fill as many spaces as possible with the snake or follow only the "turn right when encountering a wall and end if turn right twice" strategy?
$endgroup$
– Magic Octopus Urn
7 hours ago
$begingroup$
In the GIF example, when it enters the pattern why would it not turn back to fill in the other parts of the empty spaces? It was definitely possible to turn left then up then left then down then back around, but the snake chose straight down instead. Is the goal to fill as many spaces as possible with the snake or follow only the "turn right when encountering a wall and end if turn right twice" strategy?
$endgroup$
– Magic Octopus Urn
7 hours ago
$begingroup$
@MagicOctopusUrn I'm not sure I understand at what point you see the ambiguity. But, to answer your question, the goal is not to fill as much space as possible. However, the algorithm ("wall follower") is not just "turn right once or, if not possible, twice": if the snake finds itself without a wall on its left, it will have to turn left instead (keeping the "left hand" on the wall/on itself)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
@MagicOctopusUrn I'm not sure I understand at what point you see the ambiguity. But, to answer your question, the goal is not to fill as much space as possible. However, the algorithm ("wall follower") is not just "turn right once or, if not possible, twice": if the snake finds itself without a wall on its left, it will have to turn left instead (keeping the "left hand" on the wall/on itself)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
(sorry I misread your algorithm rundown)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
(sorry I misread your algorithm rundown)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
In the main example the 12th time the head turns (immediately right of the end
o) the snake could turn to it's right and still have body to its head's left. Is there an unwritten rule that turning left should be the first option it tries, or are either acceptable at that point? (or have I missed something else?)$endgroup$
– Jonathan Allan
5 hours ago
$begingroup$
In the main example the 12th time the head turns (immediately right of the end
o) the snake could turn to it's right and still have body to its head's left. Is there an unwritten rule that turning left should be the first option it tries, or are either acceptable at that point? (or have I missed something else?)$endgroup$
– Jonathan Allan
5 hours ago
$begingroup$
so the amount of fill does not have to be optimal among all possible paths?
$endgroup$
– Jonah
4 hours ago
$begingroup$
so the amount of fill does not have to be optimal among all possible paths?
$endgroup$
– Jonah
4 hours ago
|
show 3 more comments
5 Answers
5
active
oldest
votes
$begingroup$
Charcoal, 94 bytes
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θPθ…θ⌕θo≔⁰θW№KV «≔⊗⁺³⌕KV ι⟲Tι≧⁻ιθ╶»⟲TθoUMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θ
Slurp the input into a string. This could be avoided by using a less convenient input format.
Pθ…θ⌕θo
Print the input without moving the cursor, and then print up to the o again, so that the cursor ends up under it.
≔⁰θ
Initialise a variable to keep track of how much we've rotated the canvas.
W№KV «
Repeat while there's still a free space in some direction.
≔⊗⁺³⌕KV ι
Calculate whether the snake can turn left, or whether it is forced to turn right.
⟲Tι≧⁻ιθ
Rotate the canvas so that the desired direction is horizontal, and keep track of the total rotations.
╶»
Output a box drawing half line. This has the property that it is transformed by the rotation.
⟲Tθ
Rotate back to the original orientation.
o
Output the head.
UMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Transform the box drawing half lines into <>^v characters, while leaving the other characters alone.
$endgroup$
add a comment |
$begingroup$
Python 3, 347 bytes
import sys
X=0,1,0,-1
F,*Y=*X,0
L=3
g=[*map(list,sys.stdin.read().split("n"))]
e=enumerate
r,c=[[r,c]for r,R in e(g)for c,C in e(R)if C=="o"][0]
while 1:
if g[r+X[L]][c+Y[L]]==" ":F,L=L,~-L%4
elif g[r+X[F]][c+Y[F]]>" ":
if g[r-X[L]][c-Y[L]]>" ":g[r][c]="o";break
L,F=F,-~F%4
g[r][c]=">v<^"[F];r,c=r+X[F],c+Y[F]
for r in g:print("".join(r))
Try it online!
-11 bytes thanks to ArBo
$endgroup$
$begingroup$
You can golf the initialisation ofX,YandFtoX=0,1,0,-1;F,*Y=*X,0if I'm not mistaken. Also, theimport*costs you more bytes than it saves.
$endgroup$
– ArBo
6 hours ago
$begingroup$
@ArBo Oh, I thought it was saving some lol. Also that's pretty clever, thanks!
$endgroup$
– HyperNeutrino
5 hours ago
$begingroup$
I think you can save a byte with*g,=map(...). And doessys.stdin.readlines()work perhaps?
$endgroup$
– Andras Deak
3 hours ago
1
$begingroup$
Alternatively, you can probably save a few of bytes by assuming a single-line input and usinginput().
$endgroup$
– Andras Deak
2 hours ago
add a comment |
$begingroup$
Python 2, 273 253 bytes
g=input()
d=0;j=''.join
t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k]))
h='o '
while 1:
l,r=t(g,-1),t(g)
if h in l:g=l;d-=1
elif h in g:g=g.replace(h,'>v<^'[d%4]+'o')
elif h in r:g=r;d+=1
else:break
for _ in(-d%4)*' ':g=t(g)
print g
Try it online!
This working by searching the string 'o ', and replacing it by '[>v<^]o', if it is in the maze.
The same check will also be made on the rotated maze, printing the filled maze when the string isn't there anymore.
The function t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k])) is used to rotate the grid
$endgroup$
$begingroup$
No need to save''.jointo a variable, you can use it directly. Also, the parentheses in theforloop at the end are redundant.
$endgroup$
– ArBo
59 secs ago
add a comment |
$begingroup$
Ruby, 126 bytes
->sd=[q=1,1+l=s=~/$/,-1,~l];i=s=~/o/;(s[i]=">v<^"[q];s[i+d[q]]=?o;i=s=~/o/)while q=[~-q%4,q,-~q%4].find;s
Try it online!
$endgroup$
add a comment |
$begingroup$
Jelly, 72 bytes
œṣ€⁾o j€ṛị“v<^>”;”oʋ,
1ịZU$,ṭ@UZ$Ʋż2ị+-r1¤Ʋżç/€$EÐḟḢṪßƊṛ¹?
,0ÇZU$ṛ%4ɗ¡/$
Try it online!
A full program that takes the input as a list of strings and returns a list of strings with the final snake. Note the footer on TIO converts a single newline separated string into the desired input and restores the newlines at the end; this is merely for convenience.
Solution somewhat inspired by the method used by @Rod’s Python 2 answer, though implementation is very different.
$endgroup$
add a comment |
Your Answer
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%2f186909%2ffill-the-maze-with-a-wall-following-snake-until-it-gets-stuck%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Charcoal, 94 bytes
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θPθ…θ⌕θo≔⁰θW№KV «≔⊗⁺³⌕KV ι⟲Tι≧⁻ιθ╶»⟲TθoUMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θ
Slurp the input into a string. This could be avoided by using a less convenient input format.
Pθ…θ⌕θo
Print the input without moving the cursor, and then print up to the o again, so that the cursor ends up under it.
≔⁰θ
Initialise a variable to keep track of how much we've rotated the canvas.
W№KV «
Repeat while there's still a free space in some direction.
≔⊗⁺³⌕KV ι
Calculate whether the snake can turn left, or whether it is forced to turn right.
⟲Tι≧⁻ιθ
Rotate the canvas so that the desired direction is horizontal, and keep track of the total rotations.
╶»
Output a box drawing half line. This has the property that it is transformed by the rotation.
⟲Tθ
Rotate back to the original orientation.
o
Output the head.
UMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Transform the box drawing half lines into <>^v characters, while leaving the other characters alone.
$endgroup$
add a comment |
$begingroup$
Charcoal, 94 bytes
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θPθ…θ⌕θo≔⁰θW№KV «≔⊗⁺³⌕KV ι⟲Tι≧⁻ιθ╶»⟲TθoUMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θ
Slurp the input into a string. This could be avoided by using a less convenient input format.
Pθ…θ⌕θo
Print the input without moving the cursor, and then print up to the o again, so that the cursor ends up under it.
≔⁰θ
Initialise a variable to keep track of how much we've rotated the canvas.
W№KV «
Repeat while there's still a free space in some direction.
≔⊗⁺³⌕KV ι
Calculate whether the snake can turn left, or whether it is forced to turn right.
⟲Tι≧⁻ιθ
Rotate the canvas so that the desired direction is horizontal, and keep track of the total rotations.
╶»
Output a box drawing half line. This has the property that it is transformed by the rotation.
⟲Tθ
Rotate back to the original orientation.
o
Output the head.
UMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Transform the box drawing half lines into <>^v characters, while leaving the other characters alone.
$endgroup$
add a comment |
$begingroup$
Charcoal, 94 bytes
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θPθ…θ⌕θo≔⁰θW№KV «≔⊗⁺³⌕KV ι⟲Tι≧⁻ιθ╶»⟲TθoUMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θ
Slurp the input into a string. This could be avoided by using a less convenient input format.
Pθ…θ⌕θo
Print the input without moving the cursor, and then print up to the o again, so that the cursor ends up under it.
≔⁰θ
Initialise a variable to keep track of how much we've rotated the canvas.
W№KV «
Repeat while there's still a free space in some direction.
≔⊗⁺³⌕KV ι
Calculate whether the snake can turn left, or whether it is forced to turn right.
⟲Tι≧⁻ιθ
Rotate the canvas so that the desired direction is horizontal, and keep track of the total rotations.
╶»
Output a box drawing half line. This has the property that it is transformed by the rotation.
⟲Tθ
Rotate back to the original orientation.
o
Output the head.
UMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Transform the box drawing half lines into <>^v characters, while leaving the other characters alone.
$endgroup$
Charcoal, 94 bytes
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θPθ…θ⌕θo≔⁰θW№KV «≔⊗⁺³⌕KV ι⟲Tι≧⁻ιθ╶»⟲TθoUMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Try it online! Link is to verbose version of code. Explanation:
F²⊞υSW¬⁼§υ⁰§υ±¹⊞υS≔⪫υ¶θ
Slurp the input into a string. This could be avoided by using a less convenient input format.
Pθ…θ⌕θo
Print the input without moving the cursor, and then print up to the o again, so that the cursor ends up under it.
≔⁰θ
Initialise a variable to keep track of how much we've rotated the canvas.
W№KV «
Repeat while there's still a free space in some direction.
≔⊗⁺³⌕KV ι
Calculate whether the snake can turn left, or whether it is forced to turn right.
⟲Tι≧⁻ιθ
Rotate the canvas so that the desired direction is horizontal, and keep track of the total rotations.
╶»
Output a box drawing half line. This has the property that it is transformed by the rotation.
⟲Tθ
Rotate back to the original orientation.
o
Output the head.
UMKA§#<>^ov ⌕”y#╴╶╵o╷”ι
Transform the box drawing half lines into <>^v characters, while leaving the other characters alone.
answered 2 hours ago
NeilNeil
85.2k845183
85.2k845183
add a comment |
add a comment |
$begingroup$
Python 3, 347 bytes
import sys
X=0,1,0,-1
F,*Y=*X,0
L=3
g=[*map(list,sys.stdin.read().split("n"))]
e=enumerate
r,c=[[r,c]for r,R in e(g)for c,C in e(R)if C=="o"][0]
while 1:
if g[r+X[L]][c+Y[L]]==" ":F,L=L,~-L%4
elif g[r+X[F]][c+Y[F]]>" ":
if g[r-X[L]][c-Y[L]]>" ":g[r][c]="o";break
L,F=F,-~F%4
g[r][c]=">v<^"[F];r,c=r+X[F],c+Y[F]
for r in g:print("".join(r))
Try it online!
-11 bytes thanks to ArBo
$endgroup$
$begingroup$
You can golf the initialisation ofX,YandFtoX=0,1,0,-1;F,*Y=*X,0if I'm not mistaken. Also, theimport*costs you more bytes than it saves.
$endgroup$
– ArBo
6 hours ago
$begingroup$
@ArBo Oh, I thought it was saving some lol. Also that's pretty clever, thanks!
$endgroup$
– HyperNeutrino
5 hours ago
$begingroup$
I think you can save a byte with*g,=map(...). And doessys.stdin.readlines()work perhaps?
$endgroup$
– Andras Deak
3 hours ago
1
$begingroup$
Alternatively, you can probably save a few of bytes by assuming a single-line input and usinginput().
$endgroup$
– Andras Deak
2 hours ago
add a comment |
$begingroup$
Python 3, 347 bytes
import sys
X=0,1,0,-1
F,*Y=*X,0
L=3
g=[*map(list,sys.stdin.read().split("n"))]
e=enumerate
r,c=[[r,c]for r,R in e(g)for c,C in e(R)if C=="o"][0]
while 1:
if g[r+X[L]][c+Y[L]]==" ":F,L=L,~-L%4
elif g[r+X[F]][c+Y[F]]>" ":
if g[r-X[L]][c-Y[L]]>" ":g[r][c]="o";break
L,F=F,-~F%4
g[r][c]=">v<^"[F];r,c=r+X[F],c+Y[F]
for r in g:print("".join(r))
Try it online!
-11 bytes thanks to ArBo
$endgroup$
$begingroup$
You can golf the initialisation ofX,YandFtoX=0,1,0,-1;F,*Y=*X,0if I'm not mistaken. Also, theimport*costs you more bytes than it saves.
$endgroup$
– ArBo
6 hours ago
$begingroup$
@ArBo Oh, I thought it was saving some lol. Also that's pretty clever, thanks!
$endgroup$
– HyperNeutrino
5 hours ago
$begingroup$
I think you can save a byte with*g,=map(...). And doessys.stdin.readlines()work perhaps?
$endgroup$
– Andras Deak
3 hours ago
1
$begingroup$
Alternatively, you can probably save a few of bytes by assuming a single-line input and usinginput().
$endgroup$
– Andras Deak
2 hours ago
add a comment |
$begingroup$
Python 3, 347 bytes
import sys
X=0,1,0,-1
F,*Y=*X,0
L=3
g=[*map(list,sys.stdin.read().split("n"))]
e=enumerate
r,c=[[r,c]for r,R in e(g)for c,C in e(R)if C=="o"][0]
while 1:
if g[r+X[L]][c+Y[L]]==" ":F,L=L,~-L%4
elif g[r+X[F]][c+Y[F]]>" ":
if g[r-X[L]][c-Y[L]]>" ":g[r][c]="o";break
L,F=F,-~F%4
g[r][c]=">v<^"[F];r,c=r+X[F],c+Y[F]
for r in g:print("".join(r))
Try it online!
-11 bytes thanks to ArBo
$endgroup$
Python 3, 347 bytes
import sys
X=0,1,0,-1
F,*Y=*X,0
L=3
g=[*map(list,sys.stdin.read().split("n"))]
e=enumerate
r,c=[[r,c]for r,R in e(g)for c,C in e(R)if C=="o"][0]
while 1:
if g[r+X[L]][c+Y[L]]==" ":F,L=L,~-L%4
elif g[r+X[F]][c+Y[F]]>" ":
if g[r-X[L]][c-Y[L]]>" ":g[r][c]="o";break
L,F=F,-~F%4
g[r][c]=">v<^"[F];r,c=r+X[F],c+Y[F]
for r in g:print("".join(r))
Try it online!
-11 bytes thanks to ArBo
edited 5 hours ago
answered 7 hours ago
HyperNeutrinoHyperNeutrino
19.8k440151
19.8k440151
$begingroup$
You can golf the initialisation ofX,YandFtoX=0,1,0,-1;F,*Y=*X,0if I'm not mistaken. Also, theimport*costs you more bytes than it saves.
$endgroup$
– ArBo
6 hours ago
$begingroup$
@ArBo Oh, I thought it was saving some lol. Also that's pretty clever, thanks!
$endgroup$
– HyperNeutrino
5 hours ago
$begingroup$
I think you can save a byte with*g,=map(...). And doessys.stdin.readlines()work perhaps?
$endgroup$
– Andras Deak
3 hours ago
1
$begingroup$
Alternatively, you can probably save a few of bytes by assuming a single-line input and usinginput().
$endgroup$
– Andras Deak
2 hours ago
add a comment |
$begingroup$
You can golf the initialisation ofX,YandFtoX=0,1,0,-1;F,*Y=*X,0if I'm not mistaken. Also, theimport*costs you more bytes than it saves.
$endgroup$
– ArBo
6 hours ago
$begingroup$
@ArBo Oh, I thought it was saving some lol. Also that's pretty clever, thanks!
$endgroup$
– HyperNeutrino
5 hours ago
$begingroup$
I think you can save a byte with*g,=map(...). And doessys.stdin.readlines()work perhaps?
$endgroup$
– Andras Deak
3 hours ago
1
$begingroup$
Alternatively, you can probably save a few of bytes by assuming a single-line input and usinginput().
$endgroup$
– Andras Deak
2 hours ago
$begingroup$
You can golf the initialisation of
X, Y and F to X=0,1,0,-1;F,*Y=*X,0 if I'm not mistaken. Also, the import* costs you more bytes than it saves.$endgroup$
– ArBo
6 hours ago
$begingroup$
You can golf the initialisation of
X, Y and F to X=0,1,0,-1;F,*Y=*X,0 if I'm not mistaken. Also, the import* costs you more bytes than it saves.$endgroup$
– ArBo
6 hours ago
$begingroup$
@ArBo Oh, I thought it was saving some lol. Also that's pretty clever, thanks!
$endgroup$
– HyperNeutrino
5 hours ago
$begingroup$
@ArBo Oh, I thought it was saving some lol. Also that's pretty clever, thanks!
$endgroup$
– HyperNeutrino
5 hours ago
$begingroup$
I think you can save a byte with
*g,=map(...). And does sys.stdin.readlines() work perhaps?$endgroup$
– Andras Deak
3 hours ago
$begingroup$
I think you can save a byte with
*g,=map(...). And does sys.stdin.readlines() work perhaps?$endgroup$
– Andras Deak
3 hours ago
1
1
$begingroup$
Alternatively, you can probably save a few of bytes by assuming a single-line input and using
input().$endgroup$
– Andras Deak
2 hours ago
$begingroup$
Alternatively, you can probably save a few of bytes by assuming a single-line input and using
input().$endgroup$
– Andras Deak
2 hours ago
add a comment |
$begingroup$
Python 2, 273 253 bytes
g=input()
d=0;j=''.join
t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k]))
h='o '
while 1:
l,r=t(g,-1),t(g)
if h in l:g=l;d-=1
elif h in g:g=g.replace(h,'>v<^'[d%4]+'o')
elif h in r:g=r;d+=1
else:break
for _ in(-d%4)*' ':g=t(g)
print g
Try it online!
This working by searching the string 'o ', and replacing it by '[>v<^]o', if it is in the maze.
The same check will also be made on the rotated maze, printing the filled maze when the string isn't there anymore.
The function t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k])) is used to rotate the grid
$endgroup$
$begingroup$
No need to save''.jointo a variable, you can use it directly. Also, the parentheses in theforloop at the end are redundant.
$endgroup$
– ArBo
59 secs ago
add a comment |
$begingroup$
Python 2, 273 253 bytes
g=input()
d=0;j=''.join
t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k]))
h='o '
while 1:
l,r=t(g,-1),t(g)
if h in l:g=l;d-=1
elif h in g:g=g.replace(h,'>v<^'[d%4]+'o')
elif h in r:g=r;d+=1
else:break
for _ in(-d%4)*' ':g=t(g)
print g
Try it online!
This working by searching the string 'o ', and replacing it by '[>v<^]o', if it is in the maze.
The same check will also be made on the rotated maze, printing the filled maze when the string isn't there anymore.
The function t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k])) is used to rotate the grid
$endgroup$
$begingroup$
No need to save''.jointo a variable, you can use it directly. Also, the parentheses in theforloop at the end are redundant.
$endgroup$
– ArBo
59 secs ago
add a comment |
$begingroup$
Python 2, 273 253 bytes
g=input()
d=0;j=''.join
t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k]))
h='o '
while 1:
l,r=t(g,-1),t(g)
if h in l:g=l;d-=1
elif h in g:g=g.replace(h,'>v<^'[d%4]+'o')
elif h in r:g=r;d+=1
else:break
for _ in(-d%4)*' ':g=t(g)
print g
Try it online!
This working by searching the string 'o ', and replacing it by '[>v<^]o', if it is in the maze.
The same check will also be made on the rotated maze, printing the filled maze when the string isn't there anymore.
The function t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k])) is used to rotate the grid
$endgroup$
Python 2, 273 253 bytes
g=input()
d=0;j=''.join
t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k]))
h='o '
while 1:
l,r=t(g,-1),t(g)
if h in l:g=l;d-=1
elif h in g:g=g.replace(h,'>v<^'[d%4]+'o')
elif h in r:g=r;d+=1
else:break
for _ in(-d%4)*' ':g=t(g)
print g
Try it online!
This working by searching the string 'o ', and replacing it by '[>v<^]o', if it is in the maze.
The same check will also be made on the rotated maze, printing the filled maze when the string isn't there anymore.
The function t=lambda g,k=1:'n'.join(map(j,zip(*g.split('n')[::k])[::-k])) is used to rotate the grid
edited 5 hours ago
answered 5 hours ago
RodRod
16.6k41983
16.6k41983
$begingroup$
No need to save''.jointo a variable, you can use it directly. Also, the parentheses in theforloop at the end are redundant.
$endgroup$
– ArBo
59 secs ago
add a comment |
$begingroup$
No need to save''.jointo a variable, you can use it directly. Also, the parentheses in theforloop at the end are redundant.
$endgroup$
– ArBo
59 secs ago
$begingroup$
No need to save
''.join to a variable, you can use it directly. Also, the parentheses in the for loop at the end are redundant.$endgroup$
– ArBo
59 secs ago
$begingroup$
No need to save
''.join to a variable, you can use it directly. Also, the parentheses in the for loop at the end are redundant.$endgroup$
– ArBo
59 secs ago
add a comment |
$begingroup$
Ruby, 126 bytes
->sd=[q=1,1+l=s=~/$/,-1,~l];i=s=~/o/;(s[i]=">v<^"[q];s[i+d[q]]=?o;i=s=~/o/)while q=[~-q%4,q,-~q%4].find;s
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 126 bytes
->sd=[q=1,1+l=s=~/$/,-1,~l];i=s=~/o/;(s[i]=">v<^"[q];s[i+d[q]]=?o;i=s=~/o/)while q=[~-q%4,q,-~q%4].find;s
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 126 bytes
->sd=[q=1,1+l=s=~/$/,-1,~l];i=s=~/o/;(s[i]=">v<^"[q];s[i+d[q]]=?o;i=s=~/o/)while q=[~-q%4,q,-~q%4].find;s
Try it online!
$endgroup$
Ruby, 126 bytes
->sd=[q=1,1+l=s=~/$/,-1,~l];i=s=~/o/;(s[i]=">v<^"[q];s[i+d[q]]=?o;i=s=~/o/)while q=[~-q%4,q,-~q%4].find;s
Try it online!
answered 2 hours ago
Value InkValue Ink
8,395732
8,395732
add a comment |
add a comment |
$begingroup$
Jelly, 72 bytes
œṣ€⁾o j€ṛị“v<^>”;”oʋ,
1ịZU$,ṭ@UZ$Ʋż2ị+-r1¤Ʋżç/€$EÐḟḢṪßƊṛ¹?
,0ÇZU$ṛ%4ɗ¡/$
Try it online!
A full program that takes the input as a list of strings and returns a list of strings with the final snake. Note the footer on TIO converts a single newline separated string into the desired input and restores the newlines at the end; this is merely for convenience.
Solution somewhat inspired by the method used by @Rod’s Python 2 answer, though implementation is very different.
$endgroup$
add a comment |
$begingroup$
Jelly, 72 bytes
œṣ€⁾o j€ṛị“v<^>”;”oʋ,
1ịZU$,ṭ@UZ$Ʋż2ị+-r1¤Ʋżç/€$EÐḟḢṪßƊṛ¹?
,0ÇZU$ṛ%4ɗ¡/$
Try it online!
A full program that takes the input as a list of strings and returns a list of strings with the final snake. Note the footer on TIO converts a single newline separated string into the desired input and restores the newlines at the end; this is merely for convenience.
Solution somewhat inspired by the method used by @Rod’s Python 2 answer, though implementation is very different.
$endgroup$
add a comment |
$begingroup$
Jelly, 72 bytes
œṣ€⁾o j€ṛị“v<^>”;”oʋ,
1ịZU$,ṭ@UZ$Ʋż2ị+-r1¤Ʋżç/€$EÐḟḢṪßƊṛ¹?
,0ÇZU$ṛ%4ɗ¡/$
Try it online!
A full program that takes the input as a list of strings and returns a list of strings with the final snake. Note the footer on TIO converts a single newline separated string into the desired input and restores the newlines at the end; this is merely for convenience.
Solution somewhat inspired by the method used by @Rod’s Python 2 answer, though implementation is very different.
$endgroup$
Jelly, 72 bytes
œṣ€⁾o j€ṛị“v<^>”;”oʋ,
1ịZU$,ṭ@UZ$Ʋż2ị+-r1¤Ʋżç/€$EÐḟḢṪßƊṛ¹?
,0ÇZU$ṛ%4ɗ¡/$
Try it online!
A full program that takes the input as a list of strings and returns a list of strings with the final snake. Note the footer on TIO converts a single newline separated string into the desired input and restores the newlines at the end; this is merely for convenience.
Solution somewhat inspired by the method used by @Rod’s Python 2 answer, though implementation is very different.
answered 42 mins ago
Nick KennedyNick Kennedy
3,169610
3,169610
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%2f186909%2ffill-the-maze-with-a-wall-following-snake-until-it-gets-stuck%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
$begingroup$
In the GIF example, when it enters the pattern why would it not turn back to fill in the other parts of the empty spaces? It was definitely possible to turn left then up then left then down then back around, but the snake chose straight down instead. Is the goal to fill as many spaces as possible with the snake or follow only the "turn right when encountering a wall and end if turn right twice" strategy?
$endgroup$
– Magic Octopus Urn
7 hours ago
$begingroup$
@MagicOctopusUrn I'm not sure I understand at what point you see the ambiguity. But, to answer your question, the goal is not to fill as much space as possible. However, the algorithm ("wall follower") is not just "turn right once or, if not possible, twice": if the snake finds itself without a wall on its left, it will have to turn left instead (keeping the "left hand" on the wall/on itself)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
(sorry I misread your algorithm rundown)
$endgroup$
– Nicola Sap
7 hours ago
$begingroup$
In the main example the 12th time the head turns (immediately right of the end
o) the snake could turn to it's right and still have body to its head's left. Is there an unwritten rule that turning left should be the first option it tries, or are either acceptable at that point? (or have I missed something else?)$endgroup$
– Jonathan Allan
5 hours ago
$begingroup$
so the amount of fill does not have to be optimal among all possible paths?
$endgroup$
– Jonah
4 hours ago