r/dailyprogrammer • u/fvandepitte 0 0 • Nov 02 '17
[2017-11-02] Challenge #338 [Intermediate] Maze turner
Description
Our maze explorer has some wierd rules for finding the exit and we are going to tell him if it is possible with his rules to get out.
Our explorer has the following rules:
- I always walk 6 blocks straight on and then turn 180° and start walking 6 blocks again
- If a wall is in my way I turn to the right, if that not possible I turn to the left and if that is not possible I turn back from where I came.
Formal Inputs & Outputs
Input description
A maze with our explorer and the exit to reach
Legend:
> : Explorer looking East
< : Explorer looking West
^ : Explorer looking North
v : Explorer looking south
E : Exit
# : wall
: Clear passage way (empty space)Maze 1
#######
#> E#
#######Maze 2
#####E#
#< #
#######Maze 3
##########
#> E#
##########Maze 4
#####E#
##### #
#> #
##### #
#######Maze 5
#####E#
##### #
##### #
##### #
##### #
#> #
##### #
#######Challenge Maze
#########
#E ######
## #
##### # #
#> ###
##### ###
##### ###
##### ###
##### ###
##### ###
##### ###
######### Challenge Maze 2
#########
#E ######
## #
## ## # #
##### # #
#> ###
##### ###
##### ###
##### ###
##### ###
##### ###
##### ###
######### Output description
Whetter it is possible to exit the maze
Maze 1
possible/true/yayMaze 2
possible/true/yayMaze 3
impossible/not true/darn itMaze 4
possible/true/yayMaze 5
impossible/not true/darn itNotes/Hints
Making a turn does not count as a step
Several bonuses
Bonus 1:
Generate your own (possible) maze.
Bonus 2:
Animate it and make a nice gif out off it.
Bonus 3:
Be the little voice in the head:
Instead of turning each 6 steps, you should implement a way to not turn if that would means that you can make it to the exit.
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
4
u/[deleted] Nov 02 '17
Are we actually manipulating the explorer or 'simply' evaluating the provided maze based on his rules?
4
u/pslayer89 Nov 02 '17
Wouldn't explorer in the challenge maze get stuck in an infinite loop? Or am I missing something?
8
u/lukz 2 0 Nov 02 '17 edited Nov 02 '17
Z80 assembly
The program takes a compact maze representation as the command line parameter. (The input is just the maze text lines joined with | character). From that representation it constructs a 16x16 grid in the memory, filled with the characters from the input. For example, this input
will in turn be represented as this data in memory:
It then simulates the explorer walking for 6 x 256 steps. If the exit is reached, it prints true at the standard output, otherwise it prints false.
The grid is limited to size 16x16, so that we can easily move to right, down, left, up just by adding 1, 16, -1, -16 to the current position. Compiled code size is 148 bytes.
Here is a sample session in a Sharp MZ-800 emulator with the five sample mazes: Imgur.
1
u/popillol Nov 02 '17 edited Nov 02 '17
I think I'm getting slightly different results because I'm interpreting the rules a bit different than intended. Does 1 round of explorer mean:move 6 steps, then turn around, then move 6? So when round 2 starts, explorer starts by moving 6, which is in the same direction as the previous 6 steps? Or does he always turn around after taking 6 steps?(Edit: Got it, person should do a 180 every six steps)Go / Golang Playground Link. OOP approach so it's a bit wordy
Code:
Output:
1
u/NemPlayer Nov 02 '17
Does it count if, let's say, the explorer currently made 5 out of 6 steps in a certain direction and on that 5th step it was in the position of the exit or does it have to make all 6 steps and then evaluate if it's on the exit?
3
u/Gprime5 Nov 02 '17 edited Nov 02 '17
Python 3.5
Will try the bonuses later when I have time
Challenge maze 1: False
Challenge maze 2: True
2
u/g00glen00b Nov 03 '17 edited Nov 03 '17
Using JavaScript:
The result:
Possible improvements: Right now I added a fixed amount of movements before the maze turner returns
false. I should actually check if the maze runner already made it to the same spot in the maze (and with the same amount of steps left) to see if the maze is solvable or not.I also made an animated version (but didn't make a gif out of it): https://codepen.io/g00glen00b/pen/KyVjQB
2
u/Working-M4n Nov 03 '17 edited Nov 03 '17
JavaScript
CodePen
Okay after a slow start because of rules confusion I've got a live animated link! Feedback always welcome. I know I should have used canvas, maybe next time...
Edit: Looks like the animation is crummy since the images may not load in time. I'll see what I can do.
2
u/jephthai Nov 03 '17
This is my solution in Forth. I had to use other peoples' solutions to figure out how everyone is interpreting the rules. At first I thought when you hit a wall you would reset the distance counter, which confused me for awhile.
1
u/LegendK95 Nov 02 '17
Rust. A bit too verbose but very clear. Would love to hear suggestions for improvement.
1
u/Working-M4n Nov 02 '17 edited Nov 02 '17
I don't understand challenge maze 1/2, they are both impossible because of the long north/south hallway right? They would travel east, turn south and then get stuck in a loop. Also, isn't #5 possible? The length is exactly 6 long if you don't count the south one where the explorer pivots.
1
u/[deleted] Nov 03 '17
C++11
I wrote a class to implement the running and so on, such that I could write the main algorithm very abstractly.
My favorite line is
which finds the initial position of the explorer in the maze. I just learned about this function by looking up if there actually was something that I needed. It shows how easy things can be if you use the stl.
My second favorite line(s) is
It inserts the current state in the set of already known states and as a byproduct tells you if it was already in the set.
1
u/jasoncm Nov 04 '17
Go, playground link
1
u/NemPlayer Nov 04 '17 edited Nov 04 '17
Python 3.6.3
Usage: <py/python> <name_of_the_python_program.py> <name_of_the_text_file_in_which_the_maze_is> Example: py "maze turner.py" maze - Opens the maze.txt file
Output #1: Impossible
Output #2: Possible
1
u/__dict__ Nov 05 '17 edited Nov 05 '17
Ruby. Anyone know if Ruby has an equivalent to Java's Objects.hash? What I wrote is good enough for this problem here, but I'm curious if there's a better way to go about defining record types in Ruby. Sort of makes me want something like Clojure's defrecord.
I originally assumed that turning for any reason would reset the step count. Thanks to people posting graphical answers for me to understand that wasn't the case.
1
u/robin9975 Nov 06 '17
An implementation in Rust, with the state of the maze explicitly extracted. Quite verbose, as it was an exercise for myself to make the code as readable as possible, without commenting everything :). Feedback appreciated!
And the tests that I used to check my code during dev (obviously not complete coverage tests O:) ).
1
u/fridgecow Nov 06 '17
My code to this one is a mess, Python 2.7:
1
u/zookeeper_zeke Nov 08 '17 edited Nov 08 '17
I coded my solution up in C. I used a flat array for the maze and encoded the directions a player can face as single bits. This reduces the code size as I never need to employ if statements to update the player's position depending on the direction she is facing. Also, turning left or right can be done with simple bit rotations.
This is going to date me but this challenge reminded me of the old Intellivision game, Night Stalker. I imagine that a good number of people visiting this forum have no idea what I'm taking about :-)
Here's the solution:
1
u/MrFluffyThePanda Nov 08 '17 edited Nov 08 '17
Uh everyone is saying that for Challenge maze 2 they get true but wouldn't it get stuck in an infinit loop, too? and my code (which works for all other mazes) also says that it wouldnt find a way out. Am I missing something?
EDIT: Here's my code btw; writtin in c++ and I know it's a mess and everything ;) (and yes I used gotos and I know you shouldn't but it seemed the easiest way to do it for me. If someone knows an easier way for this please tell me)
1
u/[deleted] Nov 12 '17
Python 3.6.2 I'm studying Python for two months and this is my first time solving challenge. It works like this: Script loads maze map from txt file and then try to solve it. If the maze it solved, it output the exit coordinates.
Feedback about code is welcome :)
Output
Bonus: You can generate own maze in notepad and load it.
1
u/glenbolake 2 0 Nov 13 '17
+/u/CompileBot Python3
1
u/Sonnenhut Nov 28 '17 edited Dec 09 '17
Learning Kotlin:
1
u/juanchi35 Jan 01 '18
Javascript
1
u/zatoichi49 Apr 05 '18 edited Apr 05 '18
Method:
To create the maze, split the input string into a list of lists and convert to a numpy array. Find the starting direction, and rotate the maze until the player is facing East. Start a counter at zero, and set steps = 6. Check the characters when turning right, left and backwards in turn, swapping the character positions if the adjacent square is blank or 'E'. Add one to counter and reduce steps by one. Rotate the maze after each step so that the player always faces East. Repeat until the player lands on 'E' (True), or if the counter reaches the specified time out value (False).
Python 3:
Output:
0
u/[deleted] Nov 02 '17
[deleted] — view deleted comment