Advertisement
musifter

AoC day 20 (pt1), Perl

Dec 21st, 2020
2,983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.54 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. $/ = '';
  7.  
  8. my @Rev_Map = map { oct("0b" . reverse(sprintf( "%010b", $_ ))) } (0 .. 1023);
  9.  
  10. my %Tiles;
  11.  
  12. while (<>) {
  13.     my @lines = split( "\n" );
  14.     chomp @lines;
  15.  
  16.     my ($num) = ((shift @lines) =~ m#Tile (\d+):#);
  17.  
  18.     my @tile = map { [ split( // ) ] } @lines;
  19.  
  20.     my ($ident, $rot1, $rot2, $rot3)   = (0,0,0,0);  # top, left, bottom, right
  21.     foreach my $i (0 .. 9) {
  22.         $ident  = ($ident << 1) | ($tile[0][$i] eq '#');
  23.         $rot1   = ($rot1 << 1)  | ($tile[9-$i][0] eq '#');
  24.         $rot2   = ($rot2 << 1)  | ($tile[9][9-$i] eq '#');
  25.         $rot3   = ($rot3 << 1)  | ($tile[$i][9] eq '#');
  26.     }
  27.  
  28.     my $flip  = $Rev_Map[ $ident ];
  29.     my $flip1 = $Rev_Map[ $rot1  ];
  30.     my $flip2 = $Rev_Map[ $rot2  ];
  31.     my $flip3 = $Rev_Map[ $rot3  ];
  32.  
  33.     $Tiles{$num}{edge} = [ $ident, $rot1, $rot2, $rot3, $flip1, $flip, $flip3, $flip2 ];
  34.     $Tiles{$num}{tile} = \@tile;
  35. }
  36.  
  37. my @tlist = keys %Tiles;
  38.  
  39. my %Match;
  40. foreach my $t1 (0 .. $#tlist) {
  41.     foreach my $t2 ($t1 + 1 .. $#tlist) {
  42.         foreach my $i (@{$Tiles{$tlist[$t1]}{edge}}) {
  43.             if (grep { $i == $_ } @{$Tiles{$tlist[$t2]}{edge}}) {
  44.                 $Match{$tlist[$t1]}{$tlist[$t2]}++;
  45.                 $Match{$tlist[$t2]}{$tlist[$t1]}++;
  46.             }
  47.         }
  48.     }
  49. }
  50.  
  51. print "Corners:\n";
  52. my $part1 = 1;
  53. foreach my $k (keys %Match) {
  54.     if (2 == keys %{$Match{$k}}) {
  55.         print "$k\t", join( ',', keys %{$Match{$k}} ), "\n";
  56.         $part1 *= $k;
  57.     }
  58. }
  59.  
  60. print "\nPart 1: $part1\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement