]> www.fi.muni.cz Git - aoc.git/blob - 2023/28.pl
2d147f4829c4d98ef73fae13860894cf93a4745d
[aoc.git] / 2023 / 28.pl
1 #!/usr/bin/perl -w
2
3 use v5.38;
4
5 my $map; { local $/; $map = <>; }
6
7 my (@score, %seen, $round);
8 while (++$round) {
9         for (1 .. 4) {
10                 my $map1;
11                 do { # rotate the map:
12                         $map1 .= join('', reverse $map =~ /^./gm) . "\n"
13                 } while $map =~ s/^.//gm;
14                 chomp ($map = $map1);
15                 1 while $map =~ s/O(\.+)/$1O/g; # move O's to the right
16         }
17         $score[$round] +=()= substr($map, pos($map)) =~ /\n/g
18                 while $map =~ /O/g;
19
20         last if $seen{$map};
21         $seen{$map} = $round;
22 }
23
24 my $remains = (1000000000 - $round) % ($round - $seen{$map});
25 say $score[$seen{$map} + $remains];