r/dailyprogrammer • u/jnazario 2 0 • Nov 15 '17
[2017-11-14] Challenge #340 [Intermediate] Walk in a Minefield
Description
You must remotely send a sequence of orders to a robot to get it out of a minefield.
You win the game when the order sequence allows the robot to get out of the minefield without touching any mine. Otherwise it returns the position of the mine that destroyed it.
A mine field is a grid, consisting of ASCII characters like the following:
+++++++++++++
+000000000000
+0000000*000+
+00000000000+
+00000000*00+
+00000000000+
M00000000000+
+++++++++++++
The mines are represented by * and the robot by M.
The orders understandable by the robot are as follows:
- N moves the robot one square to the north
- S moves the robot one square to the south
- E moves the robot one square to the east
- O moves the robot one square to the west
- I start the the engine of the robot
- - cuts the engine of the robot
If one tries to move it to a square occupied by a wall +, then the robot stays in place.
If the robot is not started (I) then the commands are inoperative.
It is possible to stop it or to start it as many times as desired (but once enough)
When the robot has reached the exit, it is necessary to stop it to win the game.
The challenge
Write a program asking the user to enter a minefield and then asks to enter a sequence of commands to guide the robot through the field.
It displays after won or lost depending on the input command string.
Input
The mine field in the form of a string of characters, newline separated.
Output
Displays the mine field on the screen
+++++++++++
+0000000000
+000000*00+
+000000000+
+000*00*00+
+000000000+
M000*00000+
+++++++++++
Input
Commands like:
IENENNNNEEEEEEEE-
Output
Display the path the robot took and indicate if it was successful or not. Your program needs to evaluate if the route successfully avoided mines and both started and stopped at the right positions.
Bonus
Change your program to randomly generate a minefield of user-specified dimensions and ask the user for the number of mines. In the minefield, randomly generate the position of the mines. No more than one mine will be placed in areas of 3x3 cases. We will avoid placing mines in front of the entrance and exit.
Then ask the user for the robot commands.
Credit
This challenge was suggested by user /u/Preferencesoft, many thanks! If you have a challenge idea, please share it at /r/dailyprogrammer_ideas and there's a chance we'll use it.
54
u/J-Goo Nov 15 '17
Naturally.
8
u/lukz 2 0 Nov 15 '17 edited Jan 29 '18
Z80 assembly
The mine field is hardcoded in the program. The program takes as input the sequence of commands. The commands are processed, fields visited by the robot are marked with the space character. When the robot runs into a mine or the command sequence ends the program prints output and exits.
The output is a line containing yes if the task was successful or no otherwise, and a printout of the field with places visited by the robot marked with space.
Program size is 126 bytes, with the field the total size is 229 bytes.
Example sessions:
Code:
6
u/lukz 2 0 Nov 15 '17
How is the exit field defined? Can there be more than one exit?
4
u/edixon653 Nov 15 '17 edited Nov 15 '17
Here is a Python entry! I dont explain anything in comments but it is a quick mock up on my phone! Allows for preset map or randomly generated!
Python Robot Minefield
1
u/kevuno Nov 15 '17
You could make a backtracking with Dynamic Programming to automatically find the shortest path to the exit without falling into a mine. Just as a follow up for a harder question.
3
u/Kregar123 Nov 19 '17 edited Nov 19 '17
Don't know if anyone here likes Excel, but I do. I used Excel VBA. Kinda feel like I cheated a bit, using added colors for determining where the entrance and exits were for the randomly generated minefields.
Code
I didn't create the main page with VBA so the code isn't really that useful to download since you need the info and buttons on the page to run it properly. Here is a dropbox link with the file if you care to try it out.
The file can randomly generate any minefield from a 4x4 onwards, since 3x3 is pretty useless. The user can input the rows and columns they want and the number of mines they want (will default to the maximum mines if input is too high. Only 1 mine per 3x3 area starting at the top left corner inside the minefield). It has memory so if you mess up on a randomly generated minefield, just hit retry and try again. It also has the two presets included in the instructions for this project, with Preset2 being the one with the robot instructions.
Here's some screenshots of it in action.
This was pretty fun to do. I like to mess with VBA every so often but hadn't done it in a while, so I figured I'd give this a go.
edit: changed code link after making some slight changes to the code
2
u/mn-haskell-guy 1 0 Nov 17 '17 edited Nov 17 '17
Here are some test cases to consider:
The maze is:
What happens with these command sequences?
Basically - is the start square a wall, and what happens when you try to move off the maze through the end square?
2
u/suck_at_coding Nov 17 '17 edited Nov 21 '17
Go
With bonus Play link
1
u/[deleted] Nov 15 '17 edited Dec 16 '17
Ruby
Feedback welcome and appreciated! Fun challenge.
Here is a video of the program running with the (pre-configured) challenge input.
Code:
1
u/vlatkosh Nov 15 '17 edited Nov 18 '17
C++. You can enter a single grid and multiple command sequences.
1
u/Gylergin Nov 15 '17 edited Nov 17 '17
C++ with bonus: (Edit: Now follows 100% more rules!) (Edit 2: Now accounts for 100% more edge cases!)
A couple notes:
1
u/popillol Nov 15 '17
Go / Golang Playground Link. No bonus, can only have 1 exit. Runs in playground which doesn't allow stdin input.
1
u/icsEater Nov 16 '17
Python 2.7 solution. Relatively new to the language.
1
u/Scroph 0 0 Nov 16 '17
D solution, no bonus :
Output :
1
u/[deleted] Nov 16 '17 edited Nov 17 '17
F#
I decided to mess around with the OOP bits of F# and this is what I came out to (this will run in FSI)
GitHub gist for syntax highlighting
1
u/kagcc Nov 16 '17
I am not sure what is meant by
Does this mean
i <- [0..height/3]andj <- [0..width/3], there should be at most 1 mine inminefield[3*i, 3*i+1, 3*i+2][3*j,3*j+1,3*j+2]?1
u/Minolwa Nov 16 '17
OO Python3
robot.py
minefield.py
minefieldwalk.py
1
u/rqnn11 Nov 16 '17
C#
Unfortunately, my answer exceeds 10.000 characters, so I uploaded it to pastebin.
https://pastebin.com/0tZgmAV3
1
u/Taselod Nov 16 '17 edited Nov 17 '17
Javascript with ugly console logs -- but works
It also required ctrl-c to exit after entering the minefield and commands -- It was the quickest thing I could find to enter multiple lines from node..
Comments are welcome!
Output:
1
u/thestoicattack Nov 16 '17
C++17. Entirely too much space taken up by crappy switch statements to change character data into enums. Also I had to cheat a little bit to make input easier: assuming the top and bottom of the maze are entirely walls, and assuming that the exit point is a hole in the right-most wall of the maze (see
atExit).As my love affair with <algorithm> and <numeric> continues, I used
accumulateto fold "okay-ness" over the list of commands to make sure that the path doesn't hit a mine. Then we just need to check that we're really at the exit and the robot is turned off.1
u/thestoicattack Nov 16 '17
Haskell. Lots of unsafe indexing, but whatever. Straightforward translation of my C++ solution.
1
u/tomekanco Nov 16 '17 edited Nov 17 '17
Python 3.6
1
u/unlock_alchemy Nov 16 '17 edited Nov 17 '17
Here is my forth solution. The asciinema recording of the example map is here.
1
u/wenduine Nov 16 '17
Python 3.6.2
1
u/nazimkerim Nov 17 '17
Does the mine field have to be randomly generated?
1
u/[deleted] Nov 17 '17 edited Nov 18 '17
I was hoping it was going to be a pathfinder problem... maybe I'll add that later :) Also on the todo list: random maze generation based on a seed given by the user
I used Rust (as usual) for this and slightly modified the maze syntax. I added F as the target location on the other side of the minefield. This still works as usual if F is not used; it just won't be able to determine whether you've "finished."
EDIT: I've now implemented silent failure for attempting to move out-of-bounds (similar behavior to walls). I also missed the rule where the robot has to be stopped to finish - that is also fixed
1
u/[deleted] Nov 17 '17
Python 3, I feel like there's too many conditionals.
1
u/FANS4ever Nov 18 '17
Python 3 I'm new to the language and would appreciate any tips!
1
u/altanic Nov 18 '17
day (or 2) late, dollar short... C# w/ no bonus. I based this on an old maze puzzle solution we did here a few years ago... sad to say my c# has actually gotten worse. hehe :)
1
u/pop-pop-pop-pop-pop Nov 18 '17 edited Nov 18 '17
JavaScript (Clunky and bad code, slightly altered requirements, critique very welcome)
https://codepen.io/Yonkai/pen/YEYEWg?editors=0010
1
u/FrankRuben27 0 1 Nov 19 '17
In Bigloo Scheme, using Bearlib Terminal for fancy UI. Took some freedom with the tasks. Part 1:
1
u/zookeeper_zeke Nov 19 '17 edited Nov 19 '17
I coded my solution in C. It takes a minefield file as an argument and reads from stdin a list of commands. Each time a move command is executed it prints out the minefield along with the robot path as spaces. The program terminates if the robot hits a mine or reaches the exit and turns the engine off.
I lifted some code from my solution to the most recent maze problem. I represent the direction of each command as a bit in a mask which allows me to move (or look) to the next location with a single statement:
Here's the solution:
1
u/BlasphemousJoshua Nov 21 '17
Swift No bonus.
1
u/loverthehater Nov 25 '17 edited Nov 27 '17
C# .NET Core 2.0
Code:
1
u/PurlekTheGhost Nov 30 '17 edited Dec 01 '17
Java
Posted on Gist
It's not very efficient, but it works. If there’s a better way to test if something surrounding an element in a two dimensional array I would love to hear about it. Everyone else’s solution is so simple in that piece.
1
u/Sonnenhut Dec 11 '17
Kotlin
No bonus
1
u/DEN0MINAT0R Dec 21 '17
Python 3
1
u/[deleted] Jan 01 '18
Javascript - crude output to console
1
u/forlornsisu Jan 07 '18
Here is my C++ implementation. I did away with the start/stop engine thing, because it just felt unnecessary. KEY: Start Point: s End Point: o Mine: x Border: * Blank Space: [space]
1
u/juanchi35 Jan 10 '18 edited Jan 10 '18
Javascript
Full, functional code (html included).
1
u/zatoichi49 Apr 17 '18 edited Apr 20 '18
Method:
Find the index of 'M', and add boundary characters to surround the minefield string and prevent any index wrapping. Duplicate the minefield and convert to a list so that the path can be shown for each move. Create a dictionary to map each move (N, S, E, W) to it's change in index from the current position. Going through each move in the sequence, updating the maze with '.' if the move is valid. Stop if the exit is found (and ignition is off), a mine is hit, or there are no moves left (an exit square is a '0' with one or more boundary characters ('#') adjacent to it). Print the path taken.
Python 3:
Output:
-2
u/jakejet2002 Nov 15 '17
I'm having trouble can someone please post a code, I'm having trouble.I'm trying to code in c++.
0
u/[deleted] Nov 15 '17
[deleted] — view deleted comment
0
u/[deleted] Nov 17 '17
[deleted] — view deleted comment
0
u/[deleted] Nov 16 '17
[deleted] — view deleted comment