#!/usr/bin/perl use v5.35.0; use Data::Dumper; # Parse map to make a hash, keyed by antenna frequency code, # whose values are references to lists of antennas, each # represented as a coordinate pair. Note that the lists # end up ordered by coordinate pair with lower coordinates # earlier. my %antennas; my ($r, $c) = (0, 0); while (<>) { chomp; $c = 0; for (split //) { push @{$antennas{$_}}, "$r,$c" if (/[0-9A-Za-z]/); ++$c; } ++$r; } my ($height, $width) = ($r, $c); # Walk the hash to locate the antinodes. my %antinodes; for my $freq (keys %antennas) { my @list = @{$antennas{$freq}}; for my $i (0 .. $#list) { my ($ri,$ci) = split /,/, $list[$i]; for my $j ($i + 1 .. $#list) { my ($rj,$cj) = split /,/, $list[$j]; my ($rd,$cd) = ($rj - $ri, $cj - $ci); my ($r1,$c1) = ($ri - $rd, $ci - $cd); my ($r2,$c2) = ($rj + $rd, $cj + $cd); $antinodes{"$r1,$c1"} = 1 if 0 <= $r1 < $height && 0 <= $c1 < $width; $antinodes{"$r2,$c2"} = 1 if 0 <= $r2 < $height && 0 <= $c2 < $width; } } } say scalar keys %antinodes;