7
2
Follow the Path
I got directions to my friend's house, but it looks like his map might have some mistakes. He's expecting me soon, so I need some short code to figure out if I can get there.
The Challenge
The code should, when given an ASCII representation of a path as input, traverse from the start to the end, and output depending on whether there is a valid path.
Input
Input is an ASCII representation of a map. It will use only the following symbols:
Character = Meaning
(space) = Blocked area
| = Vertical Path
- = Horizontal Path
+ = Intersection
^ or v = Horizontal Bridge
< or > = Vertical Bridge
$ = Start Location
# = End Location
Traversal Rules
- Execution always starts on
$
and will end when all paths are blocked, an infinite loop, or the pointer hits#
. It is guaranteed that input will have exactly 1 of each. - The pointer starts moving to the right and down.
- Vertical paths will only connect to vertical paths, vertical bridges, or intersections.
- Horizontal paths will only connect to horizontal paths, horizontal bridges, or intersections.
- If a vertical and horizontal path meet at a non-bridge, it is considered blocked at that point.
- Bridges function according to the following:
| |
-^- OR -v- = Horizontal path going over a vertical path
| |
| |
-<- OR ->- = Vertical path going over a horizontal path
| |
- When the pointer hits an intersection, movement precedence is in the clockwise direction (up, right, down, left). The pointer will not go back the way it came.
- The program must detect/calculate infinite loops and branching paths from
+
.
Output
The length of the path, or -1
, according to the following conditions:
- If the program reaches
#
, output the path length. - If the program detects only blocked paths, return
-1
. - If the program detects an infinite loop, return
-1
.
Scoring
This is code-golf, so the shortest code in bytes wins! Standard loopholes are forbidden.
Test Cases
$
|
+-|
#+-+
= 6 (going north at `#+-` will lead to a blocked path, but going to the west is a valid path)
#-+-+
| |
$-^-+
+-+
= -1 (Will infinitely loop)
$#
= 1
#^^^^+
>
$----^-+
> +
+-+
= 20
$------+
|-----+|
--#--+|
+--+---+
+------+
= 23
$ #
= -1 (Blocked by the space)
$|#
= -1 (Can't go onto a vertical path from the sides)
$
-
#
= -1 (Can't go onto a horizontal path from the top or bottom)
$|#
|--|
++++
= -1 (No valid paths exist)
@FryAmTheEggman If you go north, you will eventually hit a blocked path, meaning that path is invalid. However, this is also a valid path to the west, which is where the 4 should be a 6. I'll add in clarification. – bigyihsuan – 2019-10-31T15:30:00.337
I see, so you automatically look ahead to see that a turn will be invalid if you would be blocked, but not if there is a loop? I'd highlight that some more since it is fine, but surprising. – FryAmTheEggman – 2019-10-31T15:34:30.693
I've put in some more clarification – bigyihsuan – 2019-10-31T15:47:50.003
Shouldn't the fourth test case result in
20
instead of19
? You first go underneath the bridge at the intersection, and after that a second time across it. So5 (east) + 4 (south) + 2 (east) + 2 (north) + 7 (west) = 20
. – Kevin Cruijssen – 2019-11-08T09:17:49.690@KevinCruijssen Yeah, that should be 20. – bigyihsuan – 2019-11-08T13:01:25.420
1@Arnauld Aight, will remove. – bigyihsuan – 2019-11-12T23:39:30.687
"Execution always starts on $ and will end when all paths are blocked, an infinite loop, or the pointer hits #." <-- Detecting an infinite loop for a small input is possibly trivial, but it's impossible to determine if the code will go into an infinite loop somewhere may require solving the Halting problem. It may still be (somewhat) possible to detect any infinite loop for any input, but I believe the question would be a lot more accessible if infinite loops weren't allowed.
– Ismael Miguel – 2019-11-13T10:29:08.263