adventofcode/2024/08/Part2.pm

48 lines
916 B
Perl
Raw Permalink Normal View History

2024-12-10 23:34:16 +00:00
use lib '.';
use 5.36.0;
use Part1;
package Part2;
use List::AllUtils qw/ uniq /;
use Math::Vector::Real;
use Algorithm::Combinatorics qw(permutations variations variations_with_repetition);
use Grid;
sub find_antinodes($grid,@group) {
my %antinodes;
my $iter = variations(\@group,2);
while( my $p = $iter->next ) {
my( $x, $y) = @$p;
my $delta = $y - $x;
$y = V(@$y);
while( $grid->is_inside(@$y) ) {
$antinodes{$y}++;
$y+=$delta;
}
}
return keys %antinodes;
}
sub solve ($grid) {
my %groups;
$grid->foreach( sub($l,$r,$content) {
return if $content eq '.';
my $g = ( $groups{$content} //= [] );
push @$g, V($l,$r);
});
my %antinodes;
for my $group ( values %groups ) {
$antinodes{$_}++ for find_antinodes($grid,@$group);
}
return scalar keys %antinodes;
}
1;