On a toroidal square grid (you can wrap around) where each cell indicates one direction (^
>
v
<
) if we pick a cell and start to walk following these directions, we will eventually get stuck in a loop.
We may end up in a different loop, depending on our starting cell.
Not all the cells we encounter constitute our terminal loop: these are said to be tributary to that loop.
Task
Given a square grid configuration, count for each loop \$L_i\$:
- How many cells is it made up with? \$n_i\$
- How many tributary cells does it have? \$t_i\$
Input
You choose the set of 4 printable characters or integers you'll use as directions.
- A square matrix having set elements as entries (can be a string)
Output
- List of \$(n_i,t_i)\$ for each \$L_i\$
The pairs can be in any order.
Example
input urdrurllruuuulrududllurdu
-vivid color: loop
-pale color: tributary
In this configuration there are 3 loops (orange, blue, green) of lengths (2, 2, 6) with (0, 10, 5) tributary cells.
output 6 5 2 10 2 0
Alternative inputs:
1232124421111421313441231
[[^,>,v,>,^],[>,<,<,>,^],[^,^,^,<,>],[^,v,^,v,<],[<,^,>,v,^]]
Valid outputs:
2 10 2 0 6 5
(2, 10), (6, 5), (2, 0)
Non valid outputs:
10 2 2 0 6 5
(0, 2), (10, 2), (6, 5)
This is code-golf, so the shortest code wins.
<
as (-1,0), etc.) to? What about complex numbers (probably a useful one here)? \$\endgroup\$