]> www.fi.muni.cz Git - aoc.git/blobdiff - 2017/44.pl
The rest of Year 2017
[aoc.git] / 2017 / 44.pl
diff --git a/2017/44.pl b/2017/44.pl
new file mode 100755 (executable)
index 0000000..6407fe4
--- /dev/null
@@ -0,0 +1,43 @@
+#!/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;