r/dailyprogrammer • u/Coder_d00d 1 3 • Sep 03 '14
[9/03/2014] Challenge #178 [Intermediate] Jumping through Hyperspace ain't like dusting Crops
Description:
You are navigator aboard the Space Pirate Bob's spaceship the Centennial Condor. Operation of the spaceship requires fuel. Bob wants to calculate a round trip to the deepest planet from his given amount of fuel he is willing to buy for a smuggling run to earn some space credits.
As navigator you need to compute the deepest planet you can make a jump to and back. Space Pirate Bob was too cheap to buy the Mark 2 spaceship navigation package for you. So you will have to improvise and code your own program to solve his problem.
Oh and by the way, the Space Pirate does not like to brack track on his routes. So the jump route to the planet cannot be the same one you take back (The Federation of Good Guy Planets will be patrolling the route you take to the planet to smuggle goods to catch you)
Good Luck, may the Code be with you.
Star Map:
You will be given a star map in the series of planet letters and fuel cost. If you take the jump route (in any direction) between these planets your spaceship will expend that many units of full. The star map has you start off on Planet A. You will need to see how far from A you can get given your below input of fuel.
The star map has the follow pairs of planets with a jump route between them and the number represents how much fuel you spend if you use it.
A B 1
A C 1
B C 2
B D 2
C D 1
C E 2
D E 2
D F 2
D G 1
E G 1
E H 1
F I 4
F G 3
G J 2
G H 3
H K 3
I J 2
I K 2input:
A value N that represents how many units the Space Pirate Bob is willing to spend his space credits on to fuel the Centennial Condor for its smuggling run.
Example:
5
Output:
The deepest route from A to a planet and back not using the same jump route (planets could be duplicated but the route back has to be unique as the one you use to get to the destination is patrolled) Display the planet and then the To route and Back route.
If no route is found - print an error message. If there is a tie, have your program decide which one to show (only 1 is needed not all)
example (using the input of 5 above):
Planet D
To: A-C-D
Back: D-B-AChallenge Inputs:
Look for routes for these fuel amounts:
- 5
- 8
- 16
7
u/XenophonOfAthens 2 1 Sep 03 '14
A clarification: what is meant by "deepest planet"? Is it "planet where the shortest distance between it and planet A is the longest"?
4
u/LiftCodeSleep Sep 03 '14
Did you intend to have a parallel edge at G-H, or is that a typo?
5
u/XenophonOfAthens 2 1 Sep 03 '14
I'm not certain if I understood the parameters of the problem correctly, but I defined "depth" to a planet as shortest path between planet A and it. That's what the depth predicate in this code does.
Also: this code disgusts me! I'm just brute-forcing like a crazy person, no clever algorithms or anything! I'm even recalculating all paths multiple times! In order to find the shortest path, I'm calculating every possible path and then sorting to find the shortest one! It's disgraceful, really.
I can almost feel my old math and programming teacher Mr. Leet look down on me in disappointment. He was from Kentucky and had a big walrus-mustache, so his disappointment was felt keenly.
Anyway, code! Here it is, in Prolog:
Output, for 5, 8 and 16 respectively:
4
u/MaximaxII Sep 04 '14
Here's a Python solution that also generates a map of the galaxy:
https://i.imgur.com/dVG2f4p.png
I've also calculated the shortest roads to every planet (just in case Space Pirate Bob should need to flee from his homebase on Planet A):
Here's the code; I got to try a new library, networkx. I like it a lot, and there's no doubt in my mind that I'll get to use another time.
Challenge #178 Intermediate - Python 3.4
1
u/darthjoey91 Sep 04 '14
So this is just a depth first search problem?
2
u/wadehn Sep 03 '14 edited Sep 04 '14
Python: I'm not sure I interpreted the instructions correctly, but I understood the challenge as: Find a target planet
Xwith maximum shortest distance fromAsuch that there are two edge-disjoint paths fromAtoXwhose sum of lengths does not exceed the given amount of fuel.I use
networkxin python to model the problem as a graph problem. I consider all target planetsXin decreasing distance fromA. To check whetherXis viable I use min-cost max flow to get two edge-disjoint paths fromAtoX(each edge has capacity1,AandXhave node capacity 2) with minimum fuel consumption.It's just somewhat tedious to extract the path from the flow in
get_pathand add node capacities.Is there a better way to model this problem?
Edit: This algorithm for my interpretation takes O(n2 * m) time. If the other interpretation to just waste as much flow as possible is correct, the problem is certainly NP-complete and we do not have any choice other than to use brute force.
Outputs:
2
u/skeeto -9 8 Sep 04 '14 edited Sep 04 '14
C99 using a brute force, exhaustive search. If I understood the problem, the goal is to return with as little fuel as possible. It reads the map on stdin and the fuel count as the first program argument.
The
route()function returns the amount of leftover fuel and stores the route it took in the passedpatharray. A return value ofINT_MAXindicates failure.Edit: here's a plot of the sample map: http://i.imgur.com/hT72DKu.png
The output of fuel=24:
1
u/qZeta Sep 04 '14
Haskell. Even though the Moore-Bellman-Ford algorithmus is slower than Dijstrka and not really appropriate for this kind of graph, I didn't really want to implement the latter in Haskell.
Output:
1
u/rozuur Sep 04 '14
For all pairs calculates paths with shortest distance
Output
1
u/Tjd__ Sep 04 '14
Went with the comment that the higher the planet name is, the further away it is considered. There is a dreadful amount of slicing, but other than that I am okay with this.
5 Deepest planet : D To: A-B-D Back: D-C-A
8 Deepest planet : G To: A-C-E-G Back: G-D-C-A
16 Deepest planet : J To: A-C-E-G-J Back: J-I-F-D-C-A
1
u/crossed_xd Sep 04 '14
I didn't necessarily complete the objective, but I did end up with an interesting solution nonetheless. What I ended up doing was, given a specific destination and fuel level, find the best possible route to the destination (i.e. shortest), and then find the way back to the originating location, all while avoiding visiting any one planet more than once (apart from the homeworld). I guess it's kind of like a traveling salesman sort of solution.
1
u/regul Sep 04 '14
Julia: here's my nasty Julia code. First third of the code is just building the adjacency matrix. The nextPlanet function is the meat and potatoes. It essentially just does a DFS, looking for paths that are longer AND use less fuel. The whole bottom third is just getting the output to print nicely.
1
u/partiallyapplied Sep 05 '14
Java
1
u/msu14 Sep 09 '14
I really enjoyed reading the various solutions to this challenge, as the subject of graphs and DFS was completely new to me. Since I am a beginner (trying my hands at an intermediate problem) I tried to go carefully through the code and (shamelessly) implement the compact Julia solution (from regul) in Ruby, the language in which I am coding now. After implementing both the Julia and Ruby code side by side; I found it interesting how easy it is to shift between these 2 languages!
travel = StarTravel.new(5)
travel.do_trip
travel = StarTravel.new(8)
travel.do_trip
travel = StarTravel.new(16)
travel.do_trip
travel = StarTravel.new(24)
travel.do_trip
1
u/Engineerbydesign Sep 10 '14
I completed mine in PYTHON:
Here is the Ouput:
[Here is a pretty picture of the planets with weighted paths] (http://i.imgur.com/XG4pdAQ.png?1) I used networkx to help build this(thanks to /u/MaximaxII since I didn't know this package existed)
1
u/KoljaKube Sep 12 '14
First, thanks for doing this! I found this subreddit a few days ago and have been itching to try some of the challenges.
So, here is my first try (done in C++(11)). Quite memory-intensive – every route is copied before a new jump is appended – but I tried to be const-correct and think that it should be parallelizable (please correct me).
And its output:
0
u/[deleted] Sep 04 '14
[deleted] — view deleted comment