From adc51097b896f6cc3a89391ce272b4798aa42a51 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 14 Dec 2023 17:45:51 +0100 Subject: [PATCH] Day 14 part 2: pure regex-based solution --- 2023/28.pl | 52 ++++++++++++++-------------------------------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/2023/28.pl b/2023/28.pl index 0699eb4..2d147f4 100755 --- a/2023/28.pl +++ b/2023/28.pl @@ -2,48 +2,24 @@ use v5.38; -my @map = map { chomp; [ split // ] } <>; -my $xmax = $#{ $map[0] }; -my $ymax = $#map; - -my %seen; -my @score; -my $round; -my $key; +my $map; { local $/; $map = <>; } +my (@score, %seen, $round); while (++$round) { for (1 .. 4) { - for my $y (0 .. $ymax) { - for my $x (0 .. $xmax) { - next if $map[$y][$x] ne 'O'; - my $y1 = $y; - while ($y1 && $map[$y1-1][$x] eq '.') { - $map[$y1][$x] = '.'; - $map[--$y1][$x] = 'O'; - } - } - } - - @map = map { # rotate clockwise - my $x = $_; - [ map { $map[$_][$x] } reverse 0 .. $ymax ] - } 0 .. $xmax; - ($xmax, $ymax) = ($ymax, $xmax); - } - - $key = join("\n", map { join('', @$_) } @map); - last if $seen{$key}; - - my $sum = 0; - for my $y (0 .. $ymax) { - for my $x (0 .. $xmax) { - $sum += $ymax + 1 - $y if $map[$y][$x] eq 'O'; - } + my $map1; + do { # rotate the map: + $map1 .= join('', reverse $map =~ /^./gm) . "\n" + } while $map =~ s/^.//gm; + chomp ($map = $map1); + 1 while $map =~ s/O(\.+)/$1O/g; # move O's to the right } + $score[$round] +=()= substr($map, pos($map)) =~ /\n/g + while $map =~ /O/g; - $seen{$key} = $round; - $score[$round] = $sum; + last if $seen{$map}; + $seen{$map} = $round; } -my $remains = (1000000000 - $round) % ($round - $seen{$key}); -say $score[$seen{$key} + $remains]; +my $remains = (1000000000 - $round) % ($round - $seen{$map}); +say $score[$seen{$map} + $remains]; -- 2.43.5