Advertisement
musifter

AoC 2024, day 25, brute force (Perl)

Dec 24th, 2024 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.67 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7. use List::Util      qw(all);
  8.  
  9. $/ = '';
  10.  
  11. my @locks;
  12. my @keys;
  13. foreach my $item (map {[split /\n/]} <>) {
  14.     my @pins;
  15.     foreach my $row (@$item) {
  16.         my @col = split( //, $row );
  17.         foreach my $pin (0 .. 4) {
  18.             $pins[$pin]++ if ($col[$pin] eq '#');
  19.         }
  20.     }
  21.  
  22.     if ($item->[0] =~ m/^#+$/) {
  23.         push( @locks, \@pins );
  24.     } else {
  25.         push( @keys,  \@pins );
  26.     }
  27. }
  28.  
  29. my $part1 = 0;
  30. foreach my $lock (@locks) {
  31.     foreach my $key (@keys) {
  32.         $part1++ if (all {$key->[$_] + $lock->[$_] <= 7} (0 .. 4));
  33.     }
  34. }
  35.  
  36. say "Part 1: $part1";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement