Advertisement
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 Math::Vector::Real;
- use Algorithm::Combinatorics qw(combinations);
- my %Grid;
- my %Antennae;
- # Read in grid, make lists of antennae of same frequency
- while (<>) {
- chomp;
- my $x = 0;
- foreach my $chr (split //) {
- $Grid{ V($.,++$x) } = $chr;
- push( $Antennae{$chr}->@*, V($.,$x) ) if ($chr ne '.');
- }
- }
- # Takes a list of harmonics from an antenna to count
- sub countAntinodes {
- my %Antinodes;
- foreach my $list (values %Antennae) {
- foreach my $pair (combinations( $list, 2 )) {
- my $delta = $pair->[0] - $pair->[1];
- foreach my $node (map { $pair->[0] + $_ * $delta } @_) {
- $Antinodes{$node}++ if (exists $Grid{$node});
- }
- }
- }
- return (scalar keys %Antinodes);
- }
- say "Part 1: ", &countAntinodes(1, -2);
- say "Part 2: ", &countAntinodes(-$. .. $.);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement