From 48892957f640e94a6940a0332fb1da6af117762d Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sat, 25 Dec 2021 06:19:27 +0100 Subject: [PATCH] Day 25: pretty straightforward --- 49.pl | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 49.pl 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"; + -- 2.43.5