Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- use List::Util qw(sum);
- $/ = '';
- my @section = map {[split /\n/]} <>;
- my $regions = pop @section;
- my @shapes;
- my @sizes;
- foreach my $desc (@section) {
- shift @$desc;
- push( @shapes, $desc );
- my $count = 0;
- foreach (@$desc) {
- my $num =()= m/#/g;
- $count += $num;
- }
- push( @sizes, $count );
- }
- my $part1 = 0;
- my @region = map { [m#(\d+)#g] } @$regions;
- foreach my $nums (@region) {
- my $width = shift @$nums;
- my $length = shift @$nums;
- my $area = $width * $length;
- my $numShapes = sum @$nums;
- my $shapesNeed = sum map { $sizes[$_] * $nums->[$_] } (0 .. $#sizes);
- my $tileGridSpots = int($width / 3) * int( $length / 3);
- next if ($area < $shapesNeed); # not enough area to fine the shapes
- if ($tileGridSpots >= $numShapes) {
- # just blindly slap the pieces down in a 3x3 grid for a solution
- $part1++;
- } else {
- # these exist for the test case, but not the input
- die "Region with potential space we didn't prove found\n";
- }
- }
- say "Part 1: $part1";
Advertisement
Add Comment
Please, Sign In to add comment