]> www.fi.muni.cz Git - aoc.git/blob - 2018/23.pl
Day 25: examining the input
[aoc.git] / 2018 / 23.pl
1 #!/usr/bin/perl -w
2
3 use v5.30;
4 use strict;
5
6 chomp (my $state = <>);
7 $state =~ s/.*: //;
8
9 scalar <>;
10
11 my %rules;
12 while (<>) {
13         chomp;
14         my ($src, $dst) = split / => /;
15         $rules{$src} = $dst;
16 }
17
18 my $off = 0;
19 for (1 .. 20) {
20         if ($state !~ /^\.\.\.\./) {
21                 $state = '....' . $state;
22                 $off -= 4;
23                 say "adding 4";
24         }
25         $state = $state . '....' if $state !~ /\.\.\.\.$/;
26
27         my $nstate = $state;
28         $nstate =~ y/#/./;
29
30         for my $p (2 .. length($state)-3) {
31                 my $src = substr($state, $p-2, 5);
32                 # die "no rule for $src" if !exists $rules{$src};
33                 substr($nstate, $p, 1) = $rules{$src} if defined $rules{$src};
34         }
35         $state = $nstate;
36         say $state;
37 }
38
39 my $sum;
40 while ($state =~ /#/g) {
41         say "at ", pos($state) + $off - 1;
42         $sum += pos($state) + $off - 1;
43 }
44 say $sum;