Skip to content

Commit

Permalink
day8
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 8, 2024
1 parent f7de1fb commit 7155ea7
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
48 changes: 48 additions & 0 deletions day08p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package day08p1

import (
"io"

"aoc/utils"
)

type position struct {
x, y int
}

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

antennaPositions := make(map[rune][]position)
antinodes := make(map[position]bool)
for i, line := range lines {
for j, c := range line {
if c == '.' {
continue
}
antennaPositions[c] = append(antennaPositions[c], position{j, i})
}
}
findAntinodes(antennaPositions, antinodes, 1, len(lines), len(lines[0]))

return len(antinodes)
}

func findAntinodes(antennaPositions map[rune][]position, antinodes map[position]bool, distance int, rows, cols int) int {
prevLen := len(antinodes)
for _, aps := range antennaPositions {
for i := 0; i < len(aps)-1; i++ {
for j := i + 1; j < len(aps); j++ {
antinode(rows, cols, position{aps[i].y - distance*(aps[j].y-aps[i].y), aps[i].x - distance*(aps[j].x-aps[i].x)}, antinodes)
antinode(rows, cols, position{aps[j].y - distance*(aps[i].y-aps[j].y), aps[j].x - distance*(aps[i].x-aps[j].x)}, antinodes)
}
}
}
return len(antinodes) - prevLen
}

func antinode(rows, cols int, position position, antinodes map[position]bool) {
if position.x >= 0 && position.x < rows && position.y >= 0 && position.y < cols {
antinodes[position] = true
}
}
44 changes: 44 additions & 0 deletions day08p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package day08p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 14},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}
50 changes: 50 additions & 0 deletions day08p2/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package day08p2

import (
"io"

"aoc/utils"
)

type position struct {
x, y int
}

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

antennaPositions := make(map[rune][]position)
antinodes := make(map[position]bool)
for i, line := range lines {
for j, c := range line {
if c == '.' {
continue
}
antennaPositions[c] = append(antennaPositions[c], position{j, i})
}
}

for distance := 0; findAntinodes(antennaPositions, antinodes, distance, len(lines), len(lines[0])) > 0; distance++ {
}

return len(antinodes)
}

func findAntinodes(antennaPositions map[rune][]position, antinodes map[position]bool, distance int, rows, cols int) int {
prevLen := len(antinodes)
for _, aps := range antennaPositions {
for i := 0; i < len(aps)-1; i++ {
for j := i + 1; j < len(aps); j++ {
antinode(rows, cols, position{aps[i].y - distance*(aps[j].y-aps[i].y), aps[i].x - distance*(aps[j].x-aps[i].x)}, antinodes)
antinode(rows, cols, position{aps[j].y - distance*(aps[i].y-aps[j].y), aps[j].x - distance*(aps[i].x-aps[j].x)}, antinodes)
}
}
}
return len(antinodes) - prevLen
}

func antinode(rows, cols int, position position, antinodes map[position]bool) {
if position.x >= 0 && position.x < rows && position.y >= 0 && position.y < cols {
antinodes[position] = true
}
}
44 changes: 44 additions & 0 deletions day08p2/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package day08p2

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 34},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}

0 comments on commit 7155ea7

Please sign in to comment.