#!/usr/bin/env tclsh9.0 namespace import ::tcl::mathop::* proc input {} { global rows cols ant set ant [dict create] set rows 0 while {[gets stdin line] >= 0} { if {$line eq ""} continue set cols [string length $line] set c 0 foreach char [split $line ""] { if {$char ne "."} { dict lappend ant $char [list $rows $c] } incr c } incr rows } } proc count {} { global rows cols ant # A location may have multiple antinodes, so we have to track them. set anti [dict create] # Iterate over antenna labels. dict for {label list} $ant { if {[llength $list] < 2} continue # Iterate over antenna pairs. for {set i 0} {$i < [llength $list] - 1} {incr i} { for {set j [+ $i 1]} {$j < [llength $list]} {incr j} { lassign [lindex $list $i] ir ic lassign [lindex $list $j] jr jc set dr [- $jr $ir] set dc [- $jc $ic] # Find all antinodes "a" (coords ar, ac) that are within # the map boundary, by iterating with a distance multiplier # from 0 to infinity, and then from -1 to -infinity, starting # at antenna "i". for {set d 0} {$d < $rows} {incr d} { set ar [- $ir [* $dr $d]] set ac [- $ic [* $dc $d]] if {$ar < 0 || $ar >= $rows || $ac < 0 || $ac >= $cols} { break } dict set anti [list $ar $ac] 1 } for {set d -1} {$d > -$rows} {incr d -1} { set ar [- $ir [* $dr $d]] set ac [- $ic [* $dc $d]] if {$ar < 0 || $ar >= $rows || $ac < 0 || $ac >= $cols} { break } dict set anti [list $ar $ac] 1 } } } } puts [dict size $anti] } input count