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 ("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 ano
character (no need to be consistent) - The end point of the snake is an
o
character
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):
Edit: note that, when in doubt, the snake should "keep its left hand" on the wall it is already on, following corners, not jumping to a wall 1-block away.
Thanks Jonathan Allan for bringing it up, and Draco18s for the explanatory snapshot above.
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#
#^<<<<<<<<<<<<<#
################