re: AoC Day 12: Subterranean Sustainability VIEW POST

Part 1 was easy. I used regular expressions to match the rules against the pots, and I needed to keep a record at what position the current pattern starts.

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my $state = <>;$state =~ s/[^#.]//g;

<>;
my %rule;
while (<>) {
chomp;
my ($left,$right) = split /\s*=>\s*/;
$rule{$left} = $right; } my$start_at = 0;
for (1 .. 20) {
substr $state, 0, 0, '...'; --$start_at;
$state .= '...'; my$new;
while ($state =~ /(?=(.....))/g) { my$before = $1; if (exists$rule{$before} && '#' eq$rule{$before}) {$new .= '#';
} else {
$new .= '.'; } }$state = $new;$state =~ s/\.+$//;$state =~ s/^(\.*)//;
$start_at += length$1;
}

my $count = 0;$count += $start_at - 1 + pos$state while $state =~ /#/g; say$count;


Running it for many more than 20 steps I discovered the pattern stays the same, only moving to the right. So I printed its value for two different numbers, the rest was just two equations:

20000a + b = 2041377
20002a + b = 2041581

b = 2041377 - 20000a

20002a + 2041377 - 20000a = 2041581
2a + 2041377 = 2041581
2a = 204
a = 102

b = 2041377 - 20000 * 102
b = 1377

code of conduct - report abuse