-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08p2.odin
68 lines (58 loc) · 1.45 KB
/
08p2.odin
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import "core:fmt"
import "core:strings"
D08P2 :: proc() {
input_string := #load("inputs/08.txt", string)
lines := strings.split(input_string, "\n", context.temp_allocator)
antinode_count := get_antinode_count_p2(lines[:len(lines) - 1])
fmt.printf("Total antinodes: %d\n", antinode_count)
}
get_antinode_count_p2 :: proc(lines: []string) -> int {
antennas := parse_antennas(lines)
antinodes := make(map[vec2]bool)
defer {
for _, antennas_at_frequency in antennas {
delete(antennas_at_frequency)
}
delete(antennas)
delete(antinodes)
}
max_x := len(lines[0])
max_y := len(lines)
// pair up antennas and get vector between them
for _, antennas_at_frequency in antennas {
for antenna1, i in antennas_at_frequency {
for antenna2, j in antennas_at_frequency {
if i >= j { // skip duplicate pairs
continue
}
// calculate vector between antennas
vec := vec2 {
antenna2.position.x - antenna1.position.x,
antenna2.position.y - antenna1.position.y,
}
check_antinode_direction(antenna1.position, vec, &antinodes, max_x, max_y)
check_antinode_direction(
antenna2.position,
vec2{-vec.x, -vec.y},
&antinodes,
max_x,
max_y,
)
}
}
}
return len(antinodes)
}
check_antinode_direction :: proc(
start: vec2,
vec: vec2,
antinodes: ^map[vec2]bool,
max_x, max_y: int,
) {
pos := start
for check_antinode(antinodes, pos, max_x, max_y) {
pos.x += vec.x
pos.y += vec.y
}
}