@@ -5,18 +5,19 @@ import (
5
5
"bufio"
6
6
"fmt"
7
7
"math"
8
- "slices"
9
8
)
10
9
11
10
const nothing = '.'
12
11
12
+ var grid [][]byte
13
+ var antennas map [byte ][]position
14
+
13
15
func main () {
14
16
file := util .FileFromArgs ()
15
17
scan := bufio .NewScanner (file )
16
18
17
- antennas : = map [byte ][]position {}
19
+ antennas = map [byte ][]position {}
18
20
19
- var grid [][]byte
20
21
var row []byte
21
22
for scan .Scan () {
22
23
row = scan .Bytes ()
@@ -33,26 +34,16 @@ func main() {
33
34
grid = append (grid , row )
34
35
}
35
36
36
- var antinodes , points []position
37
- for k := range antennas {
38
- for i := range antennas [k ] {
39
- for j := range antennas [k ] {
40
- if i == j {
41
- continue
42
- }
43
-
44
- points = calculateAntinodes (antennas [k ][i ], antennas [k ][j ])
45
- for p := range points {
46
- if slices .IndexFunc (antinodes , func (pp position ) bool { return points [p ].eq (pp ) }) == - 1 {
47
- antinodes = append (antinodes , points [p ])
48
- }
49
- }
50
-
37
+ var antinodeCount int
38
+ for y := range len (grid ) {
39
+ for x := range len (grid [0 ]) {
40
+ if isAntinode (x , y ) {
41
+ antinodeCount ++
51
42
}
52
43
}
53
44
}
54
45
55
- fmt .Println (len ( antinodes ) )
46
+ fmt .Println (antinodeCount )
56
47
}
57
48
58
49
type position struct {
@@ -63,20 +54,29 @@ func (p position) eq(q position) bool {
63
54
return p .X == q .X && p .Y == q .Y
64
55
}
65
56
66
- // calculateAntinodes will calculate all antinodes between two antennas
67
- func calculateAntinodes (c1 , c2 position ) []position {
68
- var positions []position
57
+ func isAntinode (x , y int ) bool {
58
+ var diff float64
69
59
70
- // sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
71
- d := math .Sqrt (float64 ((c1 .X - c2 .X )* (c1 .X - c2 .X ) + (c1 .Y - c2 .Y )* (c1 .Y - c2 .Y )))
60
+ for k := range antennas {
61
+ for i := range antennas [k ] {
62
+ for j := range antennas [k ] {
63
+ if i == j {
64
+ continue
65
+ }
72
66
73
- for r := range 20 {
74
- if d > float64 (r + 2 * r ) {
75
- continue // Circles don't touch
76
- }
67
+ // 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 {
77
71
78
- // TODO: calculate touch points
72
+ // Test if the triangle create by these points has surface 0, this means they are all on a single line
73
+ 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 {
74
+ return true
75
+ }
76
+ }
77
+ }
78
+ }
79
79
}
80
80
81
- return positions
81
+ return false
82
82
}
0 commit comments