#!/usr/bin/perl

use v5.30;
use strict;

my @map = map { chomp; [ split // ] } <>;

my $xmax = @{ $map[0] };
my $ymax = @map;

my $x;
for (0 .. $xmax) {
	if ($map[0][$_] eq '|') {
		$x = $_;
		last;
	}
}
my $y = 0;
my $dir = 2; # 0 up, 1 right, 2 down, 3 left

my $seen = '';
while (1) {
	my $now = $map[$y][$x];
	say "at $x,$y: '$now'";
	if ($now =~ /[A-Z]/) {
		$seen .= $now;
	} elsif ($now eq '+') {
		my $ndir;
		$ndir = 0 if $map[$y-1][$x] ne ' ' && $dir != 2;
		$ndir = 1 if $map[$y][$x+1] ne ' ' && $dir != 3;
		$ndir = 2 if $map[$y+1][$x] ne ' ' && $dir != 0;
		$ndir = 3 if $map[$y][$x-1] ne ' ' && $dir != 1;
		$dir = $ndir;
	}
	if ($dir == 0) {
		$y--;
	} elsif ($dir == 1) {
		$x++;
	} elsif ($dir == 2) {
		$y++;
	} else {
		$x--;
	}
	last if $x < 0 || $y < 0 || $x > $xmax || $y > $ymax || $map[$y][$x] eq ' ';
}

say $seen;
	
