Skip to content

Commit

Permalink
day 8 tidy up and optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Dec 8, 2024
1 parent c88b8ec commit b384926
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 93 deletions.
15 changes: 7 additions & 8 deletions 2024/08p1.odin
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ D08P1 :: proc() {

get_antinode_count :: proc(lines: []string) -> int {
antennas := parse_antennas(lines)
antinodes := [dynamic]vec2{}
antinodes := make(map[vec2]bool)
defer {
for _, antennas_at_frequency in antennas {
delete(antennas_at_frequency)
Expand All @@ -34,7 +34,7 @@ get_antinode_count :: proc(lines: []string) -> int {
for _, antennas_at_frequency in antennas {
for antenna1, i in antennas_at_frequency {
for antenna2, j in antennas_at_frequency {
if i == j {
if i >= j { // skip duplicate pairs
continue
}

Expand All @@ -55,18 +55,17 @@ get_antinode_count :: proc(lines: []string) -> int {
return len(antinodes)
}

check_antinode :: proc(antinodes: ^[dynamic]vec2, antinode: vec2, max_x: int, max_y: int) -> bool {
check_antinode :: proc(antinodes: ^map[vec2]bool, antinode: vec2, max_x: int, max_y: int) -> bool {
if antinode.x < 0 || antinode.x >= max_x || antinode.y < 0 || antinode.y >= max_y {
return false
}

for existing in antinodes^ {
if existing.x == antinode.x && existing.y == antinode.y {
return true
}
_, found := antinodes^[antinode]
if found {
return true
}

append(antinodes, antinode)
antinodes[antinode] = true

return true
}
Expand Down
63 changes: 20 additions & 43 deletions 2024/08p2.odin
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ D08P2 :: proc() {

get_antinode_count_p2 :: proc(lines: []string) -> int {
antennas := parse_antennas(lines)
antinodes := [dynamic]vec2{}
antinodes := make(map[vec2]bool)
defer {
for _, antennas_at_frequency in antennas {
delete(antennas_at_frequency)
Expand All @@ -29,7 +29,7 @@ get_antinode_count_p2 :: proc(lines: []string) -> int {
for _, antennas_at_frequency in antennas {
for antenna1, i in antennas_at_frequency {
for antenna2, j in antennas_at_frequency {
if i == j {
if i >= j { // skip duplicate pairs
continue
}

Expand All @@ -39,53 +39,30 @@ get_antinode_count_p2 :: proc(lines: []string) -> int {
antenna2.position.y - antenna1.position.y,
}

// simplify the vector (if possible) e.g. (1, 2) -> (1, 2), (2, 4) -> (1, 2)
vec = simplify_vector(vec)

// first check all valid antinodes by adding the vector to the first antenna
in_bounds := true
pos := vec2{antenna1.position.x + vec.x, antenna1.position.y + vec.y}
for in_bounds {
in_bounds = check_antinode(&antinodes, pos, max_x, max_y)
pos.x += vec.x
pos.y += vec.y
}

// then check all valid antinodes by subtracting the vector from the second antenna
// this ensures that any nodes inbetween the two antennas are counted
in_bounds = true
pos = vec2{antenna2.position.x, antenna2.position.y}
for in_bounds {
in_bounds = check_antinode(&antinodes, pos, max_x, max_y)
pos.x -= vec.x
pos.y -= vec.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)
}

simplify_vector :: proc(vec: vec2) -> vec2 {
vec_copy := vec
// find the greatest common divisor of the vector components
gcd := find_gcd(vec.x, vec.y)

// simplify the vector
vec_copy.x /= gcd
vec_copy.y /= gcd

return vec
}

find_gcd :: proc(a, b: int) -> int {
a_copy := a
b_copy := b

for b_copy != 0 {
a_copy, b_copy = b_copy, a_copy % b_copy
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
}

return a
}
56 changes: 14 additions & 42 deletions 2024/tests/08_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package main

import "core:fmt"
import "core:testing"
antenna_map :: []string {
"............",
"........0...",
".....0......",
".......0....",
"....0.......",
"......A.....",
"............",
"............",
"........A...",
".........A..",
"............",
"............",
}

@(test)
d08p1 :: proc(t: ^testing.T) {
antenna_map := []string {
"............",
"........0...",
".....0......",
".......0....",
"....0.......",
"......A.....",
"............",
"............",
"........A...",
".........A..",
"............",
"............",
}
want := 14
got := get_antinode_count(antenna_map)
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want))
Expand All @@ -28,20 +28,6 @@ d08p1 :: proc(t: ^testing.T) {

@(test)
d08p2 :: proc(t: ^testing.T) {
antenna_map := []string {
"............",
"........0...",
".....0......",
".......0....",
"....0.......",
"......A.....",
"............",
"............",
"........A...",
".........A..",
"............",
"............",
}
want := 34
got := get_antinode_count_p2(antenna_map)
testing.expect(t, got == want, fmt.aprintf("Got: %v | Want: %v", got, want))
Expand All @@ -51,20 +37,6 @@ d08p2 :: proc(t: ^testing.T) {

@(test)
test_parse_antennas :: proc(t: ^testing.T) {
antenna_map := []string {
"............",
"........0...",
".....0......",
".......0....",
"....0.......",
"......A.....",
"............",
"............",
"........A...",
".........A..",
"............",
"............",
}
got := parse_antennas(antenna_map)
want_A0 := antenna{vec2{6, 5}, 'A'}
want_03 := antenna{vec2{4, 4}, '0'}
Expand Down

0 comments on commit b384926

Please sign in to comment.