]> www.fi.muni.cz Git - aoc2020.git/blobdiff - 39.pl
Day 20: manual coding, coding, coding
[aoc2020.git] / 39.pl
diff --git a/39.pl b/39.pl
new file mode 100755 (executable)
index 0000000..2b88a89
--- /dev/null
+++ b/39.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+local $/ = "\n\n";
+
+sub invbits {
+       my ($in) = @_;
+       return (($in & 1) << 9)
+               | (($in & 2) << 7)
+               | (($in & 4) << 5)
+               | (($in & 8) << 3)
+               | (($in & 16) << 1)
+               | (($in & 32) >> 1)
+               | (($in & 64) >> 3)
+               | (($in & 128) >> 5)
+               | (($in & 256) >> 7)
+               | (($in & 512) >> 9);
+}
+
+my %tiles;
+my %sides;
+my %side2tiles;
+while (<>) {
+       my ($id, @rows) = split /\n/;
+       $id =~ s/Tile //;
+       $id =~ s/://;
+       @rows = map { y/#./10/; oct "0b$_" } @rows;
+       $tiles{$id} = \@rows;
+       $sides{$id} = [
+               $rows[0],
+               (($rows[0] & 1) << 9)
+               | (($rows[1] & 1) << 8)
+               | (($rows[2] & 1) << 7)
+               | (($rows[3] & 1) << 6)
+               | (($rows[4] & 1) << 5)
+               | (($rows[5] & 1) << 4)
+               | (($rows[6] & 1) << 3)
+               | (($rows[7] & 1) << 2)
+               | (($rows[8] & 1) << 1)
+               | (($rows[9] & 1) << 0),
+               $rows[9],
+               (($rows[0] & 512) >> 0)
+               | (($rows[1] & 512) >> 1)
+               | (($rows[2] & 512) >> 2)
+               | (($rows[3] & 512) >> 3)
+               | (($rows[4] & 512) >> 4)
+               | (($rows[5] & 512) >> 5)
+               | (($rows[6] & 512) >> 6)
+               | (($rows[7] & 512) >> 7)
+               | (($rows[8] & 512) >> 8)
+               | (($rows[9] & 512) >> 9),
+       ];
+       print "Tile <$id> sides ", join(',', @{ $sides{$id} }), "\n";
+       for my $side (@{ $sides{$id} }) {
+               push @{ $side2tiles{$side} }, $id;
+               push @{ $side2tiles{invbits($side)} }, $id;
+       }
+}
+
+my %single_ids;
+for my $side (keys %side2tiles) {
+       print "side $side: ", join(',', @{ $side2tiles{$side} }), "\n";
+       # print "inv  ", invbits($side), "\n";
+       if (scalar @{ $side2tiles{$side} } == 1
+               && scalar @{ $side2tiles{invbits($side)} } == 1) {
+               print "side $side of tile $side2tiles{$side}->[0] is single\n";
+               $single_ids{$side2tiles{$side}->[0]}++;
+       }
+       
+}
+
+my @corners = grep { $single_ids{$_} == 4 } keys %single_ids;
+print join('*', @corners), '=', eval join('*', @corners), "\n"
+