Skip to content

Commit e4dbbe4

Browse files
committed
2024.08 Part 2
1 parent 26887ea commit e4dbbe4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

2024/8-antenna/main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ func main() {
3434
grid = append(grid, row)
3535
}
3636

37-
var antinodeCount int
37+
var antinodeCount, antinodeHarmonicCount int
3838
for y := range len(grid) {
3939
for x := range len(grid[0]) {
40-
if isAntinode(x, y) {
40+
if isAntinode(x, y, false) {
4141
antinodeCount++
42+
antinodeHarmonicCount++
43+
} else if isAntinode(x, y, true) {
44+
antinodeHarmonicCount++
4245
}
4346
}
4447
}
4548

4649
fmt.Println(antinodeCount)
50+
fmt.Println(antinodeHarmonicCount)
4751
}
4852

4953
type position struct {
@@ -54,7 +58,7 @@ func (p position) eq(q position) bool {
5458
return p.X == q.X && p.Y == q.Y
5559
}
5660

57-
func isAntinode(x, y int) bool {
61+
func isAntinode(x, y int, harmonics bool) bool {
5862
var diff float64
5963

6064
for k := range antennas {
@@ -65,9 +69,11 @@ func isAntinode(x, y int) bool {
6569
}
6670

6771
// 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 {
7177

7278
// Test if the triangle create by these points has surface 0, this means they are all on a single line
7379
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

Comments
 (0)