This commit is contained in:
Borna Rajković 2024-12-08 17:56:36 +01:00
parent a16d3c95b8
commit 3cce4256cc
9 changed files with 254 additions and 0 deletions

View File

@ -24,3 +24,6 @@ var Day06Data string
//go:embed day_07/data.txt
var Day07Data string
//go:embed day_08/data.txt
var Day08Data string

50
day_08/data.txt Normal file
View File

@ -0,0 +1,50 @@
....K..........................8.................z
.....n..............r...........z.................
.......................w....8.............3...E...
.....Q.....U..4.........8.........................
...............rc...w........i....................
...........K.........i..2.........................
..................4.....i.........................
K.....n....................w...........z..........
..U......Q........................I...............
..........i.....I.....Q....g....................5E
..Q......................................5........
..........c............8......w...g..........5....
.............................I.O..................
.Z.............4....b.....................k.......
..n........4......r..g..6..c.............3........
....Z............c................................
...................................x..............
.......................................O..........
...............U...................E..........5...
.....f..........................OI3......k........
..m.......o......F.......R........x...............
m...........o..v6..3...............X..............
..............H6v.....F.g.....................W...
...........o....Fb....v...............E...........
...Z.............a................................
......U6.............V............................
.9.............b..............pTk.................
.......m........V.........H1....x.................
...m.................H....................MX......
............t.a............H......................
........Z...a............v.....1..T..p.W..X.......
.............................9...x.......p........
.....J.....................V..1................0..
...........r..j..........a............pT..........
.G..................J...N......f..................
...........G......T....B........W.e...........M...
..........j.............Rk.............M..........
.........q.............MB......R.F..1..P....X...f.
............................V....o...........h....
...........................................W......
......b......u............................e.......
.............................................0....
..CA....Gt..O........................7.....e....0.
C.u......A..9J..N........................h.....e..
uj....q..........N.2..................7...........
G....N.....uJ...............................0.....
.................B................P.......h.......
...C....q...........R.........P...................
.....q..tC....2.9.....B............P....f.........
...............2.................................7

12
day_08/example_data.txt Normal file
View File

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

View File

@ -0,0 +1,10 @@
..........
..........
..........
....a.....
..........
.....a....
..........
..........
..........
..........

View File

@ -0,0 +1,10 @@
..........
..........
..........
....a.....
........a.
.....a....
..........
......A...
..........
..........

View File

@ -0,0 +1,10 @@
T.........
...T......
.T........
..........
..........
..........
..........
..........
..........
..........

100
day_08/solution.go Normal file
View File

@ -0,0 +1,100 @@
package day_08
import (
"strings"
)
type Point struct {
X, Y int
}
// task:https://adventofcode.com/2024/day/8
func SolveBasic(input string) int {
antennas, width, height := loadAntennas(input)
antinodes := map[Point]bool{}
for _, v := range antennas {
for i := 0; i < len(v); i++ {
for j := i + 1; j < len(v); j++ {
for _, antinode := range findAntinodes(v[i], v[j], width, height) {
antinodes[antinode] = true
}
}
}
}
return len(antinodes)
}
// task:https://adventofcode.com/2024/day/8#part2
func SolveComplex(input string) int {
antennas, width, height := loadAntennas(input)
antinodes := map[Point]bool{}
for _, v := range antennas {
for i := 0; i < len(v); i++ {
for j := i + 1; j < len(v); j++ {
for _, antinode := range findAllAntinodes(v[i], v[j], width, height) {
antinodes[antinode] = true
}
}
}
}
return len(antinodes)
}
func findAntinodes(A Point, B Point, width int, height int) []Point {
dx := B.X - A.X
dy := B.Y - A.Y
points := []Point{}
p := Point{
X: A.X - dx,
Y: A.Y - dy,
}
if inGrid(p, width, height) {
points = append(points, p)
}
p = Point{
X: B.X + dx,
Y: B.Y + dy,
}
if inGrid(p, width, height) {
points = append(points, p)
}
return points
}
func findAllAntinodes(A Point, B Point, width int, height int) []Point {
dx := B.X - A.X
dy := B.Y - A.Y
points := []Point{}
for inGrid(A, width, height) {
points = append(points, Point{A.X, A.Y})
A.X -= dx
A.Y -= dy
}
for inGrid(B, width, height) {
points = append(points, Point{B.X, B.Y})
B.X += dx
B.Y += dy
}
return points
}
func inGrid(p Point, width, height int) bool {
return p.X >= 0 && p.X < width && p.Y >= 0 && p.Y < height
}
func loadAntennas(input string) (map[rune][]Point, int, int) {
results := map[rune][]Point{}
substrings := strings.Split(input, "\n")
for y, line := range substrings {
for x, char := range line {
if char != '.' {
results[char] = append(results[char], Point{x, y})
}
}
}
return results, len(substrings[0]), len(substrings)
}

54
day_08/solution_test.go Normal file
View File

@ -0,0 +1,54 @@
package day_08_test
import (
"advent_of_code_2024/day_08"
_ "embed"
"testing"
)
//go:embed example_data.txt
var ExampleData string
//go:embed example_data_01.txt
var ExampleData01 string
//go:embed example_data_02.txt
var ExampleData02 string
//go:embed example_data_03.txt
var ExampleData03 string
func TestBasicSolutionExample(t *testing.T) {
result := day_08.SolveBasic(ExampleData)
if result != 14 {
t.Fatalf("Expected 14 received %d", result)
}
}
func TestBasicSolutionExample01(t *testing.T) {
result := day_08.SolveBasic(ExampleData01)
if result != 2 {
t.Fatalf("Expected 2 received %d", result)
}
}
func TestBasicSolutionExample02(t *testing.T) {
result := day_08.SolveBasic(ExampleData02)
if result != 4 {
t.Fatalf("Expected 4 received %d", result)
}
}
func TestComplexSolutionExample03(t *testing.T) {
result := day_08.SolveComplex(ExampleData03)
if result != 9 {
t.Fatalf("Expected 9 received %d", result)
}
}
func TestComplexSolutionExample(t *testing.T) {
result := day_08.SolveComplex(ExampleData)
if result != 34 {
t.Fatalf("Expected 34 received %d", result)
}
}

View File

@ -8,6 +8,7 @@ import (
"advent_of_code_2024/day_05"
"advent_of_code_2024/day_06"
"advent_of_code_2024/day_07"
"advent_of_code_2024/day_08"
_ "embed"
"fmt"
"os"
@ -45,6 +46,10 @@ func main() {
fmt.Printf("Day 07. Basic: %d\n", day_07.SolveBasic(Day07Data)) // 20665830408335
case "07-complex":
fmt.Printf("Day 07. Complex: %d\n", day_07.SolveComplex(Day07Data)) // 20665830408335
case "08-basic":
fmt.Printf("Day 08. Basic: %d\n", day_08.SolveBasic(Day08Data)) // 20665830408335
case "08-complex":
fmt.Printf("Day 08. Complex: %d\n", day_08.SolveComplex(Day08Data)) // 20665830408335
}
}