]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 19. "practical soultion"
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 19 Dec 2020 07:29:48 +0000 (08:29 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 19 Dec 2020 07:29:48 +0000 (08:29 +0100)
37.pl [new file with mode: 0755]
38.pl [new file with mode: 0755]

diff --git a/37.pl b/37.pl
new file mode 100755 (executable)
index 0000000..97b5487
--- /dev/null
+++ b/37.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my %rules;
+my %subrules;
+my %final;
+
+while (<>) {
+       chomp;
+       last if /^$/;
+       my ($id, $rest) = /\A(\d+): (.*)\z/;
+       if ($rest =~ /"(.)"/) {
+               $final{$id} = $1;
+               next;
+       }
+       my @alts;
+       my @subalts;
+       for my $seq (split /\|/, $rest) {
+               push @alts, [ $seq =~ /(\d+)/g ];
+               push @subalts, ($seq =~ /(\d+)/g);
+       }
+       $rules{$id} = \@alts;
+       $subrules{$id} = \@subalts;
+}
+
+while (keys %rules) {
+       RULE:
+       for my $id (keys %rules) {
+               for my $subr (@{ $subrules{$id} }) {
+                       next RULE if !defined $final{$subr};
+               }
+               $final{$id} = '(?:(?:' . join(')|(?:', map {
+                       join('', map { $final{$_} } @$_)
+                       } @{ $rules{$id} }) .'))';
+               print "\$final{$id} = $final{$id}\n";
+               delete $rules{$id};
+       }
+}
+
+my $re = '\A'.$final{0}.'\z';
+
+my $count = 0;
+while (<>) {
+       chomp;
+       $count++ if ($_ =~ /$re/);
+}
+
+print "$count matched\n";
diff --git a/38.pl b/38.pl
new file mode 100755 (executable)
index 0000000..7707c8e
--- /dev/null
+++ b/38.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my %rules;
+my %subrules;
+my %final;
+
+while (<>) {
+       chomp;
+       last if /^$/;
+       my ($id, $rest) = /\A(\d+): (.*)\z/;
+       if ($rest =~ /"(.)"/) {
+               $final{$id} = $1;
+               next;
+       }
+       my @alts;
+       my @subalts;
+       for my $seq (split /\|/, $rest) {
+               push @alts, [ $seq =~ /(\d+)/g ];
+               push @subalts, ($seq =~ /(\d+)/g);
+       }
+       $rules{$id} = \@alts;
+       $subrules{$id} = \@subalts;
+}
+
+while (keys %rules) {
+       RULE:
+       for my $id (keys %rules) {
+               for my $subr (@{ $subrules{$id} }) {
+                       next RULE if !defined $final{$subr};
+               }
+               if ($id == 8) {
+                       $final{$id} = $final{42} . '+';
+                       delete $rules{$id};
+                       next RULE;
+               }
+
+               if ($id == 11) {
+                       $final{$id} = '(?:' . join('|',
+                               $final{42} . $final{31},
+                               $final{42} x 2 . $final{31} x 2,
+                               $final{42} x 3 . $final{31} x 3,
+                               $final{42} x 4 . $final{31} x 4,
+                               $final{42} x 5 . $final{31} x 5,
+                               $final{42} x 6 . $final{31} x 6,
+                               $final{42} x 7 . $final{31} x 7,
+                               $final{42} x 8 . $final{31} x 8,
+                               $final{42} x 9 . $final{31} x 9,
+                               $final{42} x 10 . $final{31} x 10,
+                               $final{42} x 11 . $final{31} x 11,
+                               $final{42} x 12 . $final{31} x 12,
+                               ) . ')';
+                       delete $rules{$id};
+                       next RULE;
+               }
+               
+               $final{$id} = '(?:(?:' . join(')|(?:', map {
+                       join('', map { $final{$_} } @$_)
+                       } @{ $rules{$id} }) .'))';
+               print "\$final{$id} = $final{$id}\n";
+               delete $rules{$id};
+       }
+}
+
+my $re = '\A'.$final{0}.'\z';
+
+my $count = 0;
+while (<>) {
+       chomp;
+       $count++ if ($_ =~ /$re/);
+}
+
+print "$count matched\n";