-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |