#!/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 search for antenna pairs with spacings that # could allow for antinodes to occur between them on one particular # reading of "regardless of distance". 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); say "($ri,$ci) ($rj,$cj)" if gcd($rd, $cd) != 1; } } } sub gcd { my ($a, $b) = @_; ($a, $b) = (abs($a), abs($b)); ($a, $b) = ($b, $a % $b) while $b; return $a; }