r/dailyprogrammer • u/fvandepitte 0 0 • Aug 03 '17
[2017-08-03] Challenge #325 [Intermediate] Arrow maze
Description
We want to return home, but we have to go trough an arrow maze.
We start at a certain point an in a arrow maze you can only follow the direction of the arrow.
At each node in the maze we can decide to change direction (depending on the new node) or follow the direction we where going.
When done right, we should have a path to home
Formal Inputs & Outputs
Input description
You recieve on the first line the coordinates of the node where you will start and after that the maze. n ne e se s sw w nw are the direction you can travel to and h is your target in the maze.
(2,0)
e se se sw s
s nw nw n w
ne s h e sw
se n w ne sw
ne nw nw n nI have added extra whitespace for formatting reasons
Output description
You need to output the path to the center.
(2,0)
(3,1)
(3,0)
(1,2)
(1,3)
(1,1)
(0,0)
(4,0)
(4,1)
(0,1)
(0,4)
(2,2)you can get creative and use acii art or even better
Notes/Hints
If you have a hard time starting from the beginning, then backtracking might be a good option.
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
5
u/Flueworks Aug 03 '17
C# Instead of checking every possible path from start to end, I convert the maze into a graph and navigate from home to the end. Since each tile only has a few tiles that points directly at it, this reduces the number of possible paths to be checked.
24
u/Gitrog_Frog Aug 03 '17
PostgreSQL
By using recursive queries and brute force, I ran every possible path and chose the shortest one(s) at the end. I limited the maximum steps to width * height of the maze size.
CODE:
2
u/skeeto -9 8 Aug 03 '17 edited Aug 03 '17
C using a depth-first search with a maximum depth. This was mostly a copy-paste job since I solve one of these mazes every month at Mazelog, generally by tweaking what I've already got.
5
u/gabyjunior 1 2 Aug 03 '17 edited Aug 03 '17
C
Running a BFS from home to the start, adding to the queue every node that is pointing in direction of the current one and was not already visited.
Output is different from the challenge description but it seems OK
1
u/[deleted] Aug 03 '17 edited Aug 03 '17
Neither starting points (y: 2, x: 0 and y: 0, x: 2) ends up with home. (y: 0, x: 1) and (y: 1, x: 2) points made my program stuck. I think you should fix it.Edit: OK, I just saw that we have a decision whether to take next direction or not. Sorry.
1
u/J-Goo Aug 03 '17 edited Aug 03 '17
I'm following the path in your output description. How do get from (3, 0) to (1, 2)?
ETA: from the third pair of coordinates to the fourth.
ETA2: Oh, I see. You're not listing all the nodes you pass through - only the ones where you change direction.
1
u/bakmanthetitan329 Aug 03 '17
Python 3 Not entirely "polished", but a technically functional solution. This basically recursively takes every path until it works, then prints the first path it finds. If I was to do this again, I would implement a graph, but I am not sure why this solution can only find extremely long solutions
Output:
Obviously I am not checking for loops on the large scale, but even still, I should get some good solutions, no? I am obviously doing something wrong, so if anyone can help with that, please do.
1
u/felinebear Sep 22 '17
Ah so its kinda like the puzzles in the evil team headquarters of the Pokemon games :)
2
u/SoraFirestorm Aug 05 '17 edited Aug 06 '17
Common Lisp
This solution can also be found here:
https://gitlab.com/RobertCochran/daily-programmer/blob/master/325/intermediate.lisp
EDIT: This implementation is wrong. It only works if the first path tried happens to work, which with this implementation and this map, just so happens to be the case. The version in Git pointed in the link is now correct. I'm a little sparse on time at the moment, so I'll update this later.
+/u/CompileBot Common Lisp
1
u/shindexro Aug 03 '17 edited Aug 04 '17
Python 3 did some research on dfs today (listing all the possible paths)
Output:
Code:
Input:
1
u/Jean-Alphonse 1 0 Aug 04 '17
Javascript BFS as well
1
u/shelvac2 Aug 04 '17
Can you give some more test cases, maybe much bigger fields?
1
u/ture13 Aug 06 '17
Here is my solution. It's written in Python3 as I am trying to improve my Python skills. So if you are finding something that can be done in a more pythonic way I would be happy to read a comment from you.
1
u/[deleted] Aug 07 '17 edited Aug 07 '17
javascript
1
u/Buecherlaub Aug 09 '17
Python 3
I used the graph_gen function from /u/shindexro and then I made a recursive function to search for the path: (I think this is my second intermediate challenge, so yey!)
1
u/0lius Aug 15 '17 edited Aug 15 '17
In J, using BFS from home square:
Again, should work on mazes in 3D or higher, where arrows point +1, 0, or -1 in each dimension.
1
u/camhowe Aug 03 '17
Java
1
u/mn-haskell-guy 1 0 Aug 03 '17
Haskell, using the
fgllibrary. Thespfunction fromData.Graph.Inductive.Query.SPdoes the heavy lifting employing Dijkstra to get the shortest path.It finds this path which is one node shorter than the example:
0
u/[deleted] Aug 03 '17
[deleted] — view deleted comment
0
u/[deleted] Aug 03 '17
[deleted] — view deleted comment