50 lines
952 B
Perl
50 lines
952 B
Perl
use lib qw/ . /;
|
|
|
|
package Part1;
|
|
|
|
use 5.36.0;
|
|
|
|
use List::AllUtils qw/ uniq /;
|
|
use Math::Vector::Real;
|
|
use Algorithm::Combinatorics qw(permutations variations);
|
|
use Grid;
|
|
|
|
no warnings qw/ uninitialized /;
|
|
|
|
sub read_input ($file) {
|
|
Grid->new( grid =>[
|
|
map { [ split '' ] }
|
|
$file->lines({chomp=>1}) ]);
|
|
}
|
|
|
|
sub find_antinodes($grid,@group) {
|
|
my %antinodes;
|
|
my $iter = variations(\@group,2);
|
|
while( my $p = $iter->next ) {
|
|
my( $x, $y) = @$p;
|
|
my $new = 2*$y-$x;
|
|
$antinodes{$new}++ if $grid->is_inside(@$new);
|
|
}
|
|
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;
|