package main

import (
	"bufio"
	"fmt"
	"os"
	"regexp"
	"strings"
)

func findAntennas(layout [][]string, antenna string) [][]int {
  antenaPositions := [][]int{}
  for i := range layout {
    for j := range layout[i] {
      if layout[i][j] == antenna {
        antenaPositions = append(antenaPositions, []int{i,j})
      }
    }
  }  
  return antenaPositions
}

func calcAntiNodes(layout [][]string, node1, node2 []int) [][]int {
  x := node1[0] - node2[0]
  y := node1[1] - node2[1]
  antinodes := [][]int{}
  for i:=0;i<len(layout);i++ {
    antinode1 := []int{node1[0] + i*x, node1[1] + i*y}
    antinode2 := []int{node2[0] - i*x, node2[1] - i*y}
    if ! (antinode1[0] >= 0 && antinode1[1] >= 0 && antinode1[0] < len(layout) && antinode1[1] < len(layout)) {
      antinode1 = []int{}
    }
    if ! (antinode2[0] >= 0 && antinode2[1] >= 0 && antinode2[0] < len(layout) && antinode2[1] < len(layout)) {
      antinode2 = []int{}
    }
    if len(antinode1) > 0 {
      antinodes = append(antinodes, antinode1)
    }
    if len(antinode2) >0 {
      antinodes = append(antinodes, antinode2)
    }
  }
  return antinodes 
}

func main() {
//  file, _ := os.Open("test")
  file, _ := os.Open("input")
  defer file.Close()
  scanner := bufio.NewScanner(file)
  regexAntenna := regexp.MustCompile(`[^.]`)
  antennas := []string{}
  layout := [][]string{}
  for scanner.Scan() {
    line := scanner.Text()
    layout = append(layout, strings.Split(line, ""))
//    //fmt.Println(line)
    foundAntennas := regexAntenna.FindAllString(line, -1)
    if len(foundAntennas) > 0 {
      antennas = append(antennas, foundAntennas...)
    }
  }
  antennaMap := make(map[string][][]int)
  for _, antenna := range antennas {
//    //fmt.Println(antenna)
    key := antenna
    antennaMap[key] = findAntennas(layout,antenna)
  }
  //fmt.Println(antennaMap)
  antinodelist := [][]int{}
  for _,v := range antennaMap {
    //fmt.Println("key: ", k, "value: ", v)
    for i:=0;i<len(v);i++ {
      for j:=0;j<len(v);j++ {
        if j>i { 
         //fmt.Println(v[i],v[j])
         antinodes := calcAntiNodes(layout, v[i], v[j])
         antinodelist = append(antinodelist, antinodes...)
        }
      }
    }
  }
  //fmt.Println(antinodelist)
  antinodeMap := make(map[[2]int]int)
  for i := range antinodelist {
    if len(antinodelist[i]) > 0 {
      key := [2]int{antinodelist[i][0], antinodelist[i][1]}
      antinodeMap[key]++
    }
  }
  fmt.Println(len(antinodeMap))
}

