]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 25: pretty straightforward
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 25 Dec 2021 05:19:27 +0000 (06:19 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 25 Dec 2021 05:19:27 +0000 (06:19 +0100)
49.pl [new file with mode: 0755]

diff --git a/49.pl b/49.pl
new file mode 100755 (executable)
index 0000000..831140f
--- /dev/null
+++ b/49.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my @m = map { chomp; [ split // ] } <>;
+
+my $xmax = @{ $m[0] };
+my $ymax = @m;
+
+sub print_map {
+       my $m = shift;
+       for my $row (@m) {
+               say join('', @$row);
+       }
+       say "";
+}
+
+my $step;
+my $moved;
+do {
+       $moved = 0;
+       my @m1;
+       for my $y (0 .. $ymax-1) {
+               for my $x (0 .. $xmax-1) {
+                       if ($m[$y][$x] eq '>') {
+                               my $nx = $x+1;
+                               $nx = 0 if $nx >= $xmax;
+                               if ($m[$y][$nx] eq '.') {
+                                       $m1[$y][$nx] = '>';
+                                       $m1[$y][$x] = '.';
+                                       $moved++;
+                               } else {
+                                       $m1[$y][$x] = $m[$y][$x];
+                               }
+                       } else {
+                               if (!defined $m1[$y][$x]) {
+                                       $m1[$y][$x] = $m[$y][$x];
+                               }
+                       }
+               }
+       }
+       @m = @m1;
+       @m1 = ();
+
+       for my $y (0 .. $ymax-1) {
+               for my $x (0 .. $xmax-1) {
+                       if ($m[$y][$x] eq 'v') {
+                               my $ny = $y+1;
+                               $ny = 0 if $ny >= $ymax;
+                               if ($m[$ny][$x] eq '.') {
+                                       $m1[$ny][$x] = 'v';
+                                       $m1[$y][$x] = '.';
+                                       $moved++;
+                               } else {
+                                       $m1[$y][$x] = $m[$y][$x];
+                               }
+                       } else {
+                               if (!defined $m1[$y][$x]) {
+                                       $m1[$y][$x] = $m[$y][$x];
+                               }
+                       }
+               }
+       }
+       @m = @m1;
+       $step++;
+       print_map(\@m);
+} while ($moved);
+
+say "$step steps";
+