From: Jan "Yenya" Kasprzak Date: Sat, 25 Dec 2021 05:19:27 +0000 (+0100) Subject: Day 25: pretty straightforward X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;ds=inline;p=aoc2021.git Day 25: pretty straightforward --- diff --git a/49.pl b/49.pl new file mode 100755 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"; +