-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_squares.go
More file actions
39 lines (34 loc) · 932 Bytes
/
detect_squares.go
File metadata and controls
39 lines (34 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package math
type DetectSquares struct {
Coordinates [][]int
Frequencies map[string]int
}
func DetectSquaresConstructor() DetectSquares {
return DetectSquares{
Coordinates: [][]int{},
Frequencies: map[string]int{},
}
}
func (this *DetectSquares) Add(point []int) {
this.Coordinates = append(this.Coordinates, point)
key := string(rune(point[0])) + "," + string(rune(point[1]))
this.Frequencies[key] += 1
}
func (this *DetectSquares) Count(point []int) int {
x, y := point[0], point[1]
result := 0
for _, coordinate := range this.Coordinates {
px, py := coordinate[0], coordinate[1]
if DetectSquaresAbs(px-x) == DetectSquaresAbs(py-y) && px != x && py != y {
key1, key2 := string(rune(x))+","+string(rune(py)), string(rune(px))+","+string(rune(y))
result += this.Frequencies[key1] * this.Frequencies[key2]
}
}
return result
}
func DetectSquaresAbs(x int) int {
if x < 0 {
return -x
}
return x
}