]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 8: permutations and y///;
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 8 Dec 2021 05:48:59 +0000 (06:48 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 8 Dec 2021 05:48:59 +0000 (06:48 +0100)
15.pl [new file with mode: 0755]
16.pl [new file with mode: 0755]

diff --git a/15.pl b/15.pl
new file mode 100755 (executable)
index 0000000..8a104b4
--- /dev/null
+++ b/15.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my $sum;
+while (<>) {
+       chomp;
+       s/.*\| //;
+       $sum += grep { my $x = length; $x == 2 || $x == 3 || $x == 4 || $x == 7; } split /\s+/;
+}
+
+say $sum;
+
+
+
+
+
diff --git a/16.pl b/16.pl
new file mode 100755 (executable)
index 0000000..fc9891e
--- /dev/null
+++ b/16.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/perl -w
+
+use v5.16;
+
+my %digits = (
+       abcefg  => 0,
+       cf      => 1,
+       acdeg   => 2,
+       acdfg   => 3,
+       bcdf    => 4,
+       abdfg   => 5,
+       abdefg  => 6,
+       acf     => 7,
+       abcdefg => 8,
+       abcdfg  => 9,
+);
+
+my @perms;
+
+sub do_perms {
+       my ($t, @vals) = @_;
+       if (@vals == 1) {
+               push @perms, $t.$vals[0];
+       } else {
+               for my $i (0 .. $#vals) {
+                       my @v1 = @vals;
+                       my $u = splice @v1, $i, 1;
+                       do_perms("$t$u", @v1);
+               }
+       }
+}
+
+do_perms ('', qw(a b c d e f g));
+
+my $sum = 0;
+ROW:
+while (<>) {
+       chomp;
+       my ($inv, $outv) = split /\s+\|\s+/;
+       my (@in) = split /\s+/, $inv;
+       my (@out) = split /\s+/, $outv;
+
+       PERM:
+       for my $perm (@perms) {
+               for my $i (@in) {
+                       my $ni = $i;
+                       eval "\$ni =~ y/abcdefg/$perm/";
+                       my $sorted = join('', sort split //, $ni);
+                       # say "i=$i ni=$ni sorted=$sorted";
+                       next PERM if !defined $digits{$sorted};
+               }
+               my $rv = '';
+               for my $o (@out) {
+                       my $no = $o;
+                       eval "\$no =~ y/abcdefg/$perm/";
+                       my $nsorted = join('', sort split //, $no);
+                       $rv .= $digits{$nsorted};
+               }
+               $sum += $rv;
+               next ROW;
+       }
+}
+
+say $sum;
+                       
+