musifter

AoC 2025 day 12 (Perl)

Dec 12th, 2025 (edited)
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.17 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(sum);
  8.  
  9. $/ = '';
  10. my @section = map {[split /\n/]} <>;
  11. my $regions = pop @section;
  12.  
  13. my @shapes;
  14. my @sizes;
  15. foreach my $desc (@section) {
  16.     shift @$desc;
  17.     push( @shapes, $desc );
  18.  
  19.     my $count = 0;
  20.     foreach (@$desc) {
  21.         my $num =()= m/#/g;
  22.         $count += $num;
  23.     }
  24.     push( @sizes, $count );
  25. }
  26.  
  27.  
  28. my $part1  = 0;
  29. my @region = map { [m#(\d+)#g] } @$regions;
  30.  
  31. foreach my $nums (@region) {
  32.     my $width  = shift @$nums;
  33.     my $length = shift @$nums;
  34.     my $area   = $width * $length;
  35.  
  36.     my $numShapes  = sum @$nums;
  37.     my $shapesNeed = sum map { $sizes[$_] * $nums->[$_] } (0 .. $#sizes);
  38.  
  39.     my $tileGridSpots = int($width / 3) * int( $length / 3);
  40.  
  41.     next if ($area < $shapesNeed);  # not enough area to fine the shapes
  42.  
  43.     if ($tileGridSpots >= $numShapes) {
  44.         # just blindly slap the pieces down in a 3x3 grid for a solution
  45.         $part1++;
  46.     } else {
  47.         # these exist for the test case, but not the input
  48.         die "Region with potential space we didn't prove found\n";
  49.     }
  50. }
  51.  
  52. say "Part 1: $part1";
Advertisement
Add Comment
Please, Sign In to add comment