#!/usr/bin/perl -w

use v5.38;
use experimental 'multidimensional';
$; = ',';

my @map = map { chomp; [ split // ] } <>;
my $xmax = $#{ $map[0] };
my $ymax = $#map;

my @dx = (1, 0, -1, 0);
my @dy = (0, 1, 0, -1);

my %seen;
my @q = [ 1, 1, 1, [1, 1] ];
while (@q) {
	my ($x, $y, $steps, @path) = @{ shift @q };

	next if $seen{$x,$y} && $seen{$x,$y} > $steps;
	$seen{$x,$y} = $steps;
	if ($x == $xmax-1 && $y == $ymax) {
		say "at $x,$y, $steps, ", join(' ', map { "[$_->[0],$_->[1]]" } @path);
		say "path len $steps";
		next;
	}
	my %visited;
	$visited{$_->[0],$_->[1]} = 1 for @path;
	for my $d (0 .. 3) {
		my $nx = $x + $dx[$d];
		my $ny = $y + $dy[$d];
		next if $map[$ny][$nx] eq '#';
		next if $ny == 0;
		next if $visited{$nx,$ny};
		next if	$d != 0 && $map[$y][$x] eq '>';
		next if	$d != 1 && $map[$y][$x] eq 'v';
		next if	$d != 2 && $map[$y][$x] eq '<';
		next if	$d != 3 && $map[$y][$x] eq '^';
		push @q, [ $nx, $ny, $steps+1, @path, [ $nx, $ny ] ];
	}
}

