--- /dev/null
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my %map;
+$; = ',';
+my $y = 0;
+my $px;
+while (<>) {
+ my $x = 0;
+ chomp;
+ for (split //) {
+ $map{$x,$y} = $_ eq '#' ? 2 : 0;
+ $x++;
+ }
+ $px = ($x-1)/2;
+ $y++;
+}
+my $py = ($y-1)/2;
+my $dir = 0; # up right down left
+
+my $sum;
+
+for (1 .. 10000000) {
+ my $state = $map{$px,$py};
+ if (!$state) {
+ $dir = 3 if --$dir < 0;
+ } elsif ($state == 1) {
+ $sum++;
+ } elsif ($state == 2) {
+ $dir = 0 if ++$dir > 3;
+ } elsif ($state == 3) {
+ $dir ^= 2;
+ }
+ $map{$px,$py} = 0 if ++$map{$px,$py} > 3;
+ $py-- if $dir == 0;
+ $px++ if $dir == 1;
+ $py++ if $dir == 2;
+ $px-- if $dir == 3;
+}
+
+say $sum;