16
1
The Challenge
Create a function that, when given an input of ASCII art (directing a path that may eventually loop), outputs the length of the loop (if there is one) and the length of the "tail" leading into the loop in one of the forms below.
Input
Your input must be passed to a function. Below is an example of a simple input.
# --> # --> #
^ |
| |
| v
# <-- #
You could visualize the above blocks like this

The "tail" is one item, while the loop is four long.
A more difficult one:
# --> # --> #
^ |
| |
| v
# --> # <-- # # --> #
^ ^ |
| | |
| | v
# --> # # <-- # <-- #
Output
You must output through STDOUT or your language's closest alternative.
Your two output integers should be the length of the tail and the length of the loop. This output can be in two forms.
- a space-delimited string:
"2 10" - an array of integers:
[2, 10]
Rules
Every block, or
#, will only have a single path away from itself.Every arrow is two line segments and one head.
The starting block will always be in the leftmost column.
The input will never be just a loop.
Example
# --> # --> # --> #
^ ^ |
| | |
| | v
# # <-- # <-- #
This one has a tail length of 2 and a loop length of 6. Below, the tail and loop are separated.
Tail
# -->
^
|
|
#
Loop
# --> # --> #
^ |
| |
| v
# <-- # <-- #
The correct outputs are [2, 6] and "2 6".
If the input is only a tail, the loop length is zero.
# --> # --> # --> #
|
|
v
<-- # <-- #
The correct outputs for the above input are [6, 0] and "6 0"
@orlp I think you're confusing input and output. – Sanchises – 2015-09-17T20:59:20.363
1Can the input have extra disconnected pieces of path? – xnor – 2015-09-17T21:00:18.393
I think the intro is confusing. It makes me think the problem will be about program analysis, whereas it is about path-finding in ASCII art. – xnor – 2015-09-17T21:01:12.013
I've removed the intro. It was a bit confusing/misleading. @xnor – Zach Gates – 2015-09-17T21:03:42.150
3
Related: Where is the arrow pointing?
– mınxomaτ – 2015-09-17T21:12:07.087I've added that piece of information under the new "Rules" section. @Zgarb – Zach Gates – 2015-09-17T21:12:39.210
This could be nice to solve in JavaScript (browser console). But there is not way to input a multiline string from STDIN or closest alternative (prompt). Why not a function call with the string as argument? – edc65 – 2015-09-17T23:26:18.897
Good point. I hadn't thought of this. Just edited the question. @edc65 – Zach Gates – 2015-09-17T23:27:14.343
I understand the input is a single multiline string. Can we assume all lines are of equal length (padded with spaces)? – Level River St – 2015-09-25T23:31:29.697
Yes. All lines are padded so that the input is rectangular. @steveverrill – Zach Gates – 2015-09-26T14:52:09.250