From 3cce4256ccfabaa19c3b509f28d2652b31c3a316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Sun, 8 Dec 2024 17:56:36 +0100 Subject: [PATCH] Day 08. --- data.go | 3 ++ day_08/data.txt | 50 +++++++++++++++++++ day_08/example_data.txt | 12 +++++ day_08/example_data_01.txt | 10 ++++ day_08/example_data_02.txt | 10 ++++ day_08/example_data_03.txt | 10 ++++ day_08/solution.go | 100 +++++++++++++++++++++++++++++++++++++ day_08/solution_test.go | 54 ++++++++++++++++++++ main.go | 5 ++ 9 files changed, 254 insertions(+) create mode 100644 day_08/data.txt create mode 100644 day_08/example_data.txt create mode 100644 day_08/example_data_01.txt create mode 100644 day_08/example_data_02.txt create mode 100644 day_08/example_data_03.txt create mode 100644 day_08/solution.go create mode 100644 day_08/solution_test.go diff --git a/data.go b/data.go index e6f2b4b..bdc35bc 100644 --- a/data.go +++ b/data.go @@ -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 diff --git a/day_08/data.txt b/day_08/data.txt new file mode 100644 index 0000000..1f21779 --- /dev/null +++ b/day_08/data.txt @@ -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 \ No newline at end of file diff --git a/day_08/example_data.txt b/day_08/example_data.txt new file mode 100644 index 0000000..de0f909 --- /dev/null +++ b/day_08/example_data.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ \ No newline at end of file diff --git a/day_08/example_data_01.txt b/day_08/example_data_01.txt new file mode 100644 index 0000000..59e975b --- /dev/null +++ b/day_08/example_data_01.txt @@ -0,0 +1,10 @@ +.......... +.......... +.......... +....a..... +.......... +.....a.... +.......... +.......... +.......... +.......... \ No newline at end of file diff --git a/day_08/example_data_02.txt b/day_08/example_data_02.txt new file mode 100644 index 0000000..2e73a87 --- /dev/null +++ b/day_08/example_data_02.txt @@ -0,0 +1,10 @@ +.......... +.......... +.......... +....a..... +........a. +.....a.... +.......... +......A... +.......... +.......... \ No newline at end of file diff --git a/day_08/example_data_03.txt b/day_08/example_data_03.txt new file mode 100644 index 0000000..023061c --- /dev/null +++ b/day_08/example_data_03.txt @@ -0,0 +1,10 @@ +T......... +...T...... +.T........ +.......... +.......... +.......... +.......... +.......... +.......... +.......... \ No newline at end of file diff --git a/day_08/solution.go b/day_08/solution.go new file mode 100644 index 0000000..74a416d --- /dev/null +++ b/day_08/solution.go @@ -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) +} diff --git a/day_08/solution_test.go b/day_08/solution_test.go new file mode 100644 index 0000000..9331286 --- /dev/null +++ b/day_08/solution_test.go @@ -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) + } +} diff --git a/main.go b/main.go index 6565abc..f9b7be3 100644 --- a/main.go +++ b/main.go @@ -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 } }