--- /dev/null
+#!/usr/bin/perl -w
+
+use v5.38;
+use experimental 'multidimensional';
+$; = ';';
+
+my @map = map { chomp; [ split // ] } <>;
+my $xmax = $#{ $map[0] };
+my $ymax = $#map;
+
+my @dirs = ([1, 0], [0, 1], [-1, 0], [0, -1]);
+
+my %seen;
+my @q = [-1, 0, 0];
+
+my @slash = (3, 2, 1, 0);
+my @bs = (1, 0, 3, 2);
+
+while (@q) {
+ my ($x, $y, $dir) = @{ shift @q };
+ my ($dx, $dy) = @{ $dirs[$dir] };
+
+ $x += $dx; $y += $dy;
+ next if $x < 0 || $y < 0 || $x > $xmax || $y > $ymax;
+
+ next if $seen{$x,$y} && ($seen{$x,$y} & (1 << $dir));
+ $seen{$x,$y} |= (1 << $dir);
+
+ my $pt = $map[$y][$x];
+ if ($pt eq '-' && ($dir == 1 || $dir == 3)) {
+ push @q, [$x, $y, 0], [$x, $y, 2];
+ } elsif ($pt eq '|' && ($dir == 0 || $dir == 2)) {
+ push @q, [$x, $y, 1], [$x, $y, 3];
+ } elsif ($pt eq '/') {
+ push @q, [$x, $y, $slash[$dir]];
+ } elsif ($pt eq '\\') {
+ push @q, [$x, $y, $bs[$dir]];
+ } else {
+ push @q, [$x, $y, $dir];
+ }
+}
+
+say scalar keys %seen;
--- /dev/null
+#!/usr/bin/perl -w
+
+use v5.38;
+use experimental 'multidimensional';
+$; = ';';
+
+my @map = map { chomp; [ split // ] } <>;
+my $xmax = $#{ $map[0] };
+my $ymax = $#map;
+
+my @dirs = ([1, 0], [0, 1], [-1, 0], [0, -1]);
+
+my @slash = (3, 2, 1, 0);
+my @bs = (1, 0, 3, 2);
+
+sub energ ($pt) {
+ my %seen;
+ my @q = $pt;
+ while (@q) {
+ my ($x, $y, $dir) = @{ shift @q };
+ my ($dx, $dy) = @{ $dirs[$dir] };
+
+ $x += $dx; $y += $dy;
+ next if $x < 0 || $y < 0 || $x > $xmax || $y > $ymax;
+
+ next if $seen{$x,$y} && $seen{$x,$y} & (1 << $dir);
+ $seen{$x,$y} |= (1 << $dir);
+
+ my $pt = $map[$y][$x];
+ if ($pt eq '-' && ($dir == 1 || $dir == 3)) {
+ push @q, [$x, $y, 0], [$x, $y, 2];
+ } elsif ($pt eq '|' && ($dir == 0 || $dir == 2)) {
+ push @q, [$x, $y, 1], [$x, $y, 3];
+ } elsif ($pt eq '/') {
+ push @q, [$x, $y, $slash[$dir]];
+ } elsif ($pt eq '\\') {
+ push @q, [$x, $y, $bs[$dir]];
+ } else {
+ push @q, [$x, $y, $dir];
+ }
+ }
+ return scalar keys %seen;
+}
+
+my $max = 0;
+
+for (0 .. $xmax) {
+ my $cnt = energ([$_, -1, 1]);
+ $max = $cnt if $cnt > $max;
+ $cnt = energ([$_, $ymax+1, 3]);
+ $max = $cnt if $cnt > $max;
+}
+
+for (0 .. $ymax) {
+ my $cnt = energ([-1, $_, 0]);
+ $max = $cnt if $cnt > $max;
+ $cnt = energ([$xmax+1, $_, 2]);
+ $max = $cnt if $cnt > $max;
+}
+
+say $max;