Back
Close

We're going in circles!

Statement

 Goal

There is a w by h grid of arrows represented by: "^", ">", "v", "<". Empty cells are represented by periods: ".".

A loop of arrows is a set of arrows that loop back on themselves when moving from one arrow to the closest arrow in the direction that it points. If an arrow points outside the grid, it is not part of a loop.

The goal is to count the number_of_loops in the grid.
Input
line 1: w and h separated by a space (width and height, respectively).
The next h lines: The grid of arrows and empty cells.
Output
The number_of_loops in the grid.
Constraints
0 < w, h <= 20
The grid only contains the characters: ^>v<.
Example
Input
5 4
>.>.v
....v
....v
^<<<<
Output
1

Tags
graph traversal

Difficulty
Medium

Test cases
A simple loop Test
Input
5 4 >.>.v ....v ....v ^<<<<
Output
1

A simple loop validator Validator
Input
4 4 .v<. v<^< >v>^ .>^.
Output
1

Not a loop Test
Input
5 3 v...< ..... >.v.^
Output
0

Not a loop validator Validator
Input
5 3 >...v ..... ^.v.<
Output
0

Still not a loop Test
Input
5 4 v...< .>.v. .^^<. >.v.^
Output
0

Overlapping loops validator Validator
Input
5 5 >v.>. .>v^< ..>v. ...>^ ^^^^^
Output
0

Overlapping loops Test
Input
9 5 .v..<.... >..v...>v ......v.< .>..^.>^. ^..<.....
Output
3

Overlapping loops validator Validator
Input
7 7 ..>...v .>...v. >...v.. ....... .^..<.. ..^..<. ^.....<
Output
1

Mad face >.< Test
Input
5 3 v>.<v .>.<. ^>.<^
Output
5

Mad face >.< validator Validator
Input
5 5 .vvv. >...< >...< >...< .^^^.
Output
6

Wall of text Test
Input
5 5 >>>>> >>>>> >>>>> >>>>> >>>>>
Output
0

Wall of text validator Validator
Input
10 10 .......... .......... .......... .......... .......... .......... .......... .......... .......... ..........
Output
0

Multiple inputs Test
Input
8 8 <<<<<<<^ v......^ v.vvv<.^ v.>>v<.^ v.>^<<.^ v.>^^^.^ v......^ v>>>>>>>
Output
1

Multiple inputs validator Validator
Input
10 5 <v...<...^ ..<.v.>.>. ..vv<^<.<> ...>>^v.^^ .>......^.
Output
1

Gobbledygook Test
Input
20 20 .................... ^>..v...v.......<.>. ........<..>..v..... v.....^......v..^... .^.........^.^><.... >..v....<........... .v....<.^.v.v....<.. <.>.^...^....^v..... ...><.^..........^^. .>^v........^....^.. .v.......v.<>v.^.... ^.vvvvvvvvvvv......v ..>>>>>><<<<<....<.. ...>.>....>.^....^.. ..........<.^....... ..^.....v...<...>.<. .>.......^......v... .........>...>...... ...^.......<..<..... .^......<..<....>.^.
Output
7

Gobbledygook validator Validator
Input
20 20 .v.^...vv..>>>>v.<^. <.<.<<^....^<<<<.^.. .>.^....v^^<<.v^<^.. <<<.v...v^.^^....v>. <>^^<<<.v.<^^>v..... .>>^..>>>v.>^>.v...v ^.<<.>^.v.<^.vv...v. <<^^.v^v>..^^v.v.v.. <.v^..^..v.^...<v<<< ^^>^vv^>..>.vv>>.>>^ .^v.vv^<<<^.<>..>v.v .^<<.>>.v<...^^..>v> ..^..^v.<..v.^.vv... .>.>v.>>v..<v...v^>^ .^<.<<..<>>.>..>>^.. .<>..v<^vv>^.^..^<<. vv<..>>.v>..v^<<^<^. <<.<>>v>vv..<...^^.. vv..>>v.vv^>>vv>^>.. <<^.^..vv.^..v.>^<>^
Output
4

More goobledygook Test
Input
20 20 .<<...<..<<<.>.....^ ..v.v........^..^..^ .............^<.<.>^ ^...v..>..^..^...>v. ^.v.v>..>...^..v.v.. ..v.<^.<.<....v<<..v ..v....^........^... ...<.....>.....v^... ......^>.v<.<..>^.>. >>...v^...^v....^..< ^.v.......^v.....v<. .....>.........v..<. .....^.....<...<^... .......>.....vv...<< ^..>>..v..v>^<.<.... ..<..>.v^....vv...v. ...^..^<^<....>^.... .v<..<^<.......v.... <.......>>.......... ..v>.>...v<v.....vv.
Output
1

More goobledygook validator Validator
Input
20 20 ..^.<<<vv<<.<...^<>v v>.>v...<.^..v..^.>> .^.<v<<<<.<<>vv<.^.^ v...<>>.>.v^vv<<<<<^ v^..<.>^v^.^.v<v>v>. v^>.^.^<.^...v^.<.^> v.^.^....>.>v<^..v^^ >>..^v<<.^..vv.v.>^< .....<^<..<<<<..<>>. ..v.<.>>.>.v.<^<.vv> ..<.<.^.v>^v<<...<>v ..>>v.^.....<^^..... .<..v^^.vv...>..>.v> ^v^<<>^<v.v.<>>.^>v< <.^v<^.>v<vv.>>>>.v. v>^<>.v.<.v<.^..<.v. <^...v<<..<<.>^.^.<< <^v.<v>>>>.^^<<<v... .^vv^v<.v<v^v.^.>v.. <...^<vvv>v^....<v..
Output
3

Solution language

Solution

Stub generator input