From: Jan "Yenya" Kasprzak Date: Sat, 19 Dec 2020 07:29:48 +0000 (+0100) Subject: Day 19. "practical soultion" X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=aoc2020.git;a=commitdiff_plain;h=bafab1d5fe18827b4d193a33372d0b22bb6d0c67 Day 19. "practical soultion" --- diff --git a/37.pl b/37.pl new file mode 100755 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 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";