@@ -34,16 +34,20 @@ func main() {
34
34
grid = append (grid , row )
35
35
}
36
36
37
- var antinodeCount int
37
+ var antinodeCount , antinodeHarmonicCount int
38
38
for y := range len (grid ) {
39
39
for x := range len (grid [0 ]) {
40
- if isAntinode (x , y ) {
40
+ if isAntinode (x , y , false ) {
41
41
antinodeCount ++
42
+ antinodeHarmonicCount ++
43
+ } else if isAntinode (x , y , true ) {
44
+ antinodeHarmonicCount ++
42
45
}
43
46
}
44
47
}
45
48
46
49
fmt .Println (antinodeCount )
50
+ fmt .Println (antinodeHarmonicCount )
47
51
}
48
52
49
53
type position struct {
@@ -54,7 +58,7 @@ func (p position) eq(q position) bool {
54
58
return p .X == q .X && p .Y == q .Y
55
59
}
56
60
57
- func isAntinode (x , y int ) bool {
61
+ func isAntinode (x , y int , harmonics bool ) bool {
58
62
var diff float64
59
63
60
64
for k := range antennas {
@@ -65,9 +69,11 @@ func isAntinode(x, y int) bool {
65
69
}
66
70
67
71
// Are towers on double distances?
68
- diff = math .Sqrt (float64 (util .IntPow (x - antennas [k ][i ].X , 2 )+ util .IntPow (y - antennas [k ][i ].Y , 2 ))) /
69
- math .Sqrt (float64 (util .IntPow (x - antennas [k ][j ].X , 2 )+ util .IntPow (y - antennas [k ][j ].Y , 2 )))
70
- if diff == 2 || diff == .5 {
72
+ if ! harmonics {
73
+ diff = math .Sqrt (float64 (util .IntPow (x - antennas [k ][i ].X , 2 )+ util .IntPow (y - antennas [k ][i ].Y , 2 ))) /
74
+ math .Sqrt (float64 (util .IntPow (x - antennas [k ][j ].X , 2 )+ util .IntPow (y - antennas [k ][j ].Y , 2 )))
75
+ }
76
+ if harmonics || diff == 2 || diff == .5 {
71
77
72
78
// Test if the triangle create by these points has surface 0, this means they are all on a single line
73
79
if x * (antennas [k ][i ].Y - antennas [k ][j ].Y )+ antennas [k ][i ].X * (antennas [k ][j ].Y - y )+ antennas [k ][j ].X * (y - antennas [k ][i ].Y ) == 0 {
0 commit comments