r/dailyprogrammer • u/Coder_d00d 1 3 • Dec 03 '14
[2014-12-3] Challenge #191 [Intermediate] Space Probe. Alright Alright Alright.
Description:
NASA has contracted you to program the AI of a new probe. This new probe must navigate space from a starting location to an end location. The probe will have to deal with Asteroids and Gravity Wells. Hopefully it can find the shortest path.
Map and Path:
This challenge requires you to establish a random map for the challenge. Then you must navigate a probe from a starting location to an end location.
Map:
You are given N -- you generate a NxN 2-D map (yes space is 3-D but for this challenge we are working in 2-D space)
- 30% of the spots are "A" asteroids
- 10% of the spots are "G" gravity wells (explained below)
- 60% of the spots are "." empty space.
When you generate the map you must figure out how many of each spaces is needed to fill the map. The map must then be randomly populated to hold the amount of Gravity Wells and Asteroids based on N and the above percentages.
N and Obstacles
As n changes so does the design of your random space map. Truncate the amount of obstacles and its always a min size of 1. (So say N is 11 so 121 spaces. At 10% for wells you need 12.1 or just 12 spots) N can be between 2 and 1000. To keep it simple you will assume every space is empty then populate the random Asteroids and Gravity wells (no need to compute the number of empty spaces - they will just be the ones not holding a gravity well or asteroid)
Asteroids
Probes cannot enter the space of an Asteroid. It will just be destroyed.
Empty Spaces
Probes can safely cross space by the empty spaces of space. Beware of gravity wells as described below.
Gravity Wells
Gravity wells are interesting. The Space itself is so dense it cannot be travelled in. The adjacent spaces of a Gravity well are too strong and cannot be travelled in. Therefore you might see this.
. = empty space, G = gravity well
.....
.....
..G..
.....
.....But due to the gravity you cannot pass (X = unsafe)
.....
.XXX.
.XGX.
.XXX.
.....You might get Gravity wells next to each other. They do not effect each other but keep in mind the area around them will not be safe to travel in.
......
.XXXX.
.XGGX.
.XXXX.
......Probe Movement:
Probes can move 8 directions. Up, down, left, right or any of the 4 adjacent corners. However there is no map wrapping. Say you are at the top of the map you cannot move up to appear on the bottom of the map. Probes cannot fold space. And for whatever reason we are contained to only the spots on the map even thou space is infinite in any direction.
Output:
Must show the final Map and shortest safe route on the map.
- . = empty space
- S = start location
- E = end location
- G = gravity well
- A = Asteroid
- O = Path.
If you fail to get to the end because of no valid path you must travel as far as you can and show the path. Note that the probe path was terminated early due to "No Complete Path" error.
Challenge Input:
using (row, col) for coordinates in space.
Find solutions for:
- N = 10, start = (0,0) end = (9,9)
- N = 10, start = (9, 0) end = (0, 9)
- N= 50, start = (0,0) end = (49, 49)
Map Obstacle %
I generated a bunch of maps and due to randomness you will get easy ones or hard ones. I suggest running your solutions many times to see your outcomes. If you find the solution is always very straight then I would increase your asteroid and gravity well percentages. Or if you never get a good route then decrease the obstacle percentages.
Challenge Theme Music:
If you need inspiration for working on this solution listen to this in the background to help you.
https://www.youtube.com/watch?v=4PL4kzsrVX8
Or
37
u/adrian17 1 4 Dec 03 '14 edited Dec 15 '14
...I think I overdid it. I did an A* implementation with graphics in C++11. Also, with some extra features like manually adding/removing asteroids with mouse, restarting the map, changing distance type for heuteristics (can change performance a lot, but can also lead to less optimal results) and switching between instant and real-time pathfinding.
Repo: https://github.com/adrian17/AsteroidPathFinder/
Screenshot:
http://puu.sh/dfLvR/41a32356f8.png
EDIT: and a short presentation: http://puu.sh/dgw8S/1076b411fb.webm
Red are asteroids, shades of black are gravity and gravity wells, the darker green fields are all the analyzed paths. The screenshot was done at 10% asteroid and 5% gravity wells IIRC.
Actually, I had an A* implementation with some optimizations laying around so I had a bit of a head start here :P
By the way, can anyone suggest how to
std::sortavectorofvectorswhile avoiding copy construction? This should be possible as you could swap them just by swapping pointers to internal memory, but the compiler spams copy constructors there so much that anstd::list(!) was actually more efficient.4
u/lukz 2 0 Dec 03 '14
vbscript
I have done my solution in vbscript and hope that it is readable. If there are any questions about how it works, please ask, I'll be glad to answer.
The input values are coded at the beginning of the program - map size 20x20, start at (1,1) and end at (20,20).
I have set the probability of gravity well at 9 % and the probability of asteroid at 10 %, otherwise I get too many maps without solution.
Example output:
Code:
2
u/mpatraw Dec 03 '14
C. Uses a simple BFS. Here's the gist and the ideone to play with. I've tested up to N = 10000 which takes about 5 seconds on my machine.
2
u/jnazario 2 0 Dec 04 '14 edited Dec 04 '14
ok, F#. this time with a proper shortest path calculator -- A* (before i had a very naive one that often failed). whee.
this is quite possibly the most FP-ish code i have yet written, i'm proud of that. there's a bug where it'll not detect that it can't complete ... hmm ... EDIT fixed the bug, changed the code.
2
u/dohaqatar7 1 1 Dec 05 '14
Java A* Implementation
Code: https://gist.github.com/anonymous/ff41b55c4566313a91f2
Large maps are computed with negligible delays.
Sample Output: 200x200
20X20:
It's not perfect yet, but it reliably finds (what looks like) the shortest path.
1
u/Godspiral 3 3 Dec 03 '14 edited Dec 03 '14
In J, just the mapgen, where 1 is well, 2 asteroid, 3 empty. 4 is start 5 is end.
probabilities are left param, grid x y are right param:
initmap =: ] $ 5 (_1}) 4 (0}) [: +/ +/@:[ >/ [: ? */@:] $ +/@:[
rest is borrowed from game of life solution.
gwellexpand turns adjacent squares into gwells, and so can be applied multiple times to simulate expansion or different rules.
with fixed seed and small map, the density of gwells becomes too high for most maps to get a path. example:
with smaller frequencies, even narrow maps are not overwhelmed by double gwellexpands:
1
u/Acidom Dec 03 '14 edited Dec 04 '14
Python, done via BFS. Does not edit the matrix, but rather prints out the shortest path coordinates.
Code: https://gist.github.com/Onebrownsound/ab3a8a99f340b3b884a4
Edit: Updated it so it now has the ability to print the path itself separately and also on the map if desired. It was annoying seeing the screen scroll for seconds on large N's (>100).
1
u/[deleted] Dec 04 '14 edited Dec 22 '18
deleted 0.9072 What is ^^^this?
1
u/LuckyShadow Dec 04 '14
Python 3
https://gist.github.com/DaveAtGit/36fae312b7be7da41a01#file-space_probe-py
I am using a Breadth-First-Search with a simple dictionary for the path. The problem with this is, that, upon failure, it is not guaranteed to show the best possible path. It is just the longest possible path.
You can set, as requested, size, start and end values. The percentages for asteroids and gravity wells are also customizable. For my tests, the default values weren't that good...
Usage (you can also call it with
-hfor help):Output:
For better readability i recommend a symbol-string like
"., SEFX-"and viewingSpace.SYMB_IMP. The resulting output looks like this (|added to view borders):Feedback is always welcome. :)
1
u/fvandepitte 0 0 Dec 04 '14
In c# with A* search pattern.<br> No cutting corners!!
I didn't get a good result with the 30% astroids and 10% gravity so I changed it a bit.
1
u/[deleted] Dec 05 '14 edited Dec 05 '14
Python 3.
I'm new to this path-finding business, but after reading around and some trial and error I arrived at an implementation of A*. It was pretty cool to learn some new stuff whilst making this!
1
u/binaryblade Dec 08 '14
golang, comment if you will but this is my solution. When you encounter the no solution case it just prints all accessible locations because there is not canonical closest.
1
u/DorffMeister Dec 10 '14
My Groovy solution.
1
u/ICanCountTo0b1010 Dec 17 '14
Here's my solution in C++11:
github folder
this was a ton of fun to make, I was happy to write my first implementation of the A-Star algorithm. Next step is to add graphics.
Output:
1
u/broken_broken_ May 13 '15 edited May 13 '15
Javacript es6 (nodejs). Late to the party. Like most solutions, A*. Most of the time and code are due to the lack of rich data structure in JS :(
Output: https://github.com/gaultier/nodeWork/blob/master/191-i-output.txt
1
u/britboy3456 Dec 03 '14
Java
I used map size 20x20, start at (1,1) and end at (20,20). I used a 5% chance of gravity well, and a 10% chance of asteroid. These can be changed at the top of the code.
It is not necessarily the fastest route, I was a bit lazy...
Code: http://hastebin.com/hohelaqasa.cpp
Sample successful output:
Sample failed output: