Day08
This commit is contained in:
80
2024/gareth/day08/day08.go
Normal file
80
2024/gareth/day08/day08.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package day08
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Coordinate struct {
|
||||||
|
Row int
|
||||||
|
Col int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(input string) int {
|
||||||
|
grid, antennaMap := parseInput(input)
|
||||||
|
result := findAntinodes(grid, antennaMap, true)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(input string) int {
|
||||||
|
grid, antennaMap := parseInput(input)
|
||||||
|
result := findAntinodes(grid, antennaMap, false)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseInput(input string) ([][]rune, map[rune][]Coordinate) {
|
||||||
|
lines := strings.Split(input, "\n")
|
||||||
|
rowCount := len(lines)
|
||||||
|
colCount := len(lines[0])
|
||||||
|
grid := make([][]rune, rowCount)
|
||||||
|
antennaMap := make(map[rune][]Coordinate)
|
||||||
|
|
||||||
|
for row := 0; row < rowCount; row++ {
|
||||||
|
grid[row] = []rune(lines[row])
|
||||||
|
for col := 0; col < colCount; col++ {
|
||||||
|
frequency := grid[row][col]
|
||||||
|
if frequency != '.' {
|
||||||
|
antennaMap[frequency] = append(antennaMap[frequency], Coordinate{Row: row, Col: col})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return grid, antennaMap
|
||||||
|
}
|
||||||
|
|
||||||
|
func findAntinodes(grid [][]rune, antennaMap map[rune][]Coordinate, onlySpecial bool) int {
|
||||||
|
rowCount := len(grid)
|
||||||
|
colCount := len(grid[0])
|
||||||
|
uniqueAntinodes := make(map[Coordinate]struct{})
|
||||||
|
|
||||||
|
for row := 0; row < rowCount; row++ {
|
||||||
|
for col := 0; col < colCount; col++ {
|
||||||
|
for _, antennaPositions := range antennaMap {
|
||||||
|
for i, firstAntenna := range antennaPositions {
|
||||||
|
for j, secondAntenna := range antennaPositions {
|
||||||
|
//I know I know thats too many for loops
|
||||||
|
if i != j {
|
||||||
|
distance1 := int(math.Abs(float64(row-firstAntenna.Row)) + math.Abs(float64(col-firstAntenna.Col)))
|
||||||
|
distance2 := int(math.Abs(float64(row-secondAntenna.Row)) + math.Abs(float64(col-secondAntenna.Col)))
|
||||||
|
|
||||||
|
rowDiff1 := row - firstAntenna.Row
|
||||||
|
rowDiff2 := row - secondAntenna.Row
|
||||||
|
colDiff1 := col - firstAntenna.Col
|
||||||
|
colDiff2 := col - secondAntenna.Col
|
||||||
|
|
||||||
|
if rowDiff1*colDiff2 == rowDiff2*colDiff1 {
|
||||||
|
if (distance1 == 2*distance2 || distance1*2 == distance2) && onlySpecial {
|
||||||
|
uniqueAntinodes[Coordinate{Row: row, Col: col}] = struct{}{}
|
||||||
|
} else if !onlySpecial {
|
||||||
|
uniqueAntinodes[Coordinate{Row: row, Col: col}] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(uniqueAntinodes)
|
||||||
|
}
|
||||||
39
2024/gareth/day08/day08_test.go
Normal file
39
2024/gareth/day08/day08_test.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package day08
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPart1(t *testing.T) {
|
||||||
|
r := Part1(`............
|
||||||
|
........0...
|
||||||
|
.....0......
|
||||||
|
.......0....
|
||||||
|
....0.......
|
||||||
|
......A.....
|
||||||
|
............
|
||||||
|
............
|
||||||
|
........A...
|
||||||
|
.........A..
|
||||||
|
............
|
||||||
|
............`)
|
||||||
|
assert.Equal(t, 14, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPart2(t *testing.T) {
|
||||||
|
r := Part2(`............
|
||||||
|
........0...
|
||||||
|
.....0......
|
||||||
|
.......0....
|
||||||
|
....0.......
|
||||||
|
......A.....
|
||||||
|
............
|
||||||
|
............
|
||||||
|
........A...
|
||||||
|
.........A..
|
||||||
|
............
|
||||||
|
............`)
|
||||||
|
assert.Equal(t, 11387, r)
|
||||||
|
}
|
||||||
50
2024/gareth/day08/input.txt
Normal file
50
2024/gareth/day08/input.txt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
........................E...j......W..........L...
|
||||||
|
............................O........E.........L..
|
||||||
|
..q......O...........l....................K.......
|
||||||
|
............q...................HM......W.........
|
||||||
|
................................1..H...........IW.
|
||||||
|
....................5.............................
|
||||||
|
..........k........M...wl............6............
|
||||||
|
.....O.......w...k.....5.8..l......K.........o.6..
|
||||||
|
.......k....w.........5.........R.....o........K..
|
||||||
|
.....q..X..............j........E...I.........K...
|
||||||
|
............O..........E........................H.
|
||||||
|
................Mn.h2.w.p....................H....
|
||||||
|
..................p.......a............j.....L....
|
||||||
|
.....X...l.p.....................m.........W..6...
|
||||||
|
..Xq................A..................R..m.......
|
||||||
|
.........................i..........a..........R..
|
||||||
|
...........u.....................a........I.....2.
|
||||||
|
k..............A..n.........R.................o...
|
||||||
|
................n.................Qo..............
|
||||||
|
..........u.A.........h........2..................
|
||||||
|
...5.......Y.....p...............iN...............
|
||||||
|
1...x.....................i.......................
|
||||||
|
........M..............2.....Qi...................
|
||||||
|
...............................I..e...............
|
||||||
|
......u......A...........m..........h.............
|
||||||
|
.......1...........U.............Qm.......j.......
|
||||||
|
.......X.......................................9..
|
||||||
|
.....u........U.......Y...........................
|
||||||
|
.............................h.e..................
|
||||||
|
..................4....e......Q.....L....N........
|
||||||
|
.1..................4.......................y8....
|
||||||
|
.........Y................................8.N.....
|
||||||
|
............P.0J...........3..........8y..........
|
||||||
|
....V3P..........J................................
|
||||||
|
............U..P...7x...........e.................
|
||||||
|
....................J...............r...9.........
|
||||||
|
.........0.V......Y...............................
|
||||||
|
...............V.4................................
|
||||||
|
..........V..........................n............
|
||||||
|
..............v........7..........................
|
||||||
|
...........U..........J.......7...................
|
||||||
|
.....v........7..........................a........
|
||||||
|
.......................................r..........
|
||||||
|
...........0.......x................y.............
|
||||||
|
............6..v.x.....................N..........
|
||||||
|
...........P......................................
|
||||||
|
........3.......................r......4..........
|
||||||
|
..............3......................y............
|
||||||
|
................................................9.
|
||||||
|
.................................................9
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"aoc2024/day14"
|
"aoc2024/day08"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
data, _ := os.ReadFile("day14/input.txt")
|
data, _ := os.ReadFile("day08/input.txt")
|
||||||
fmt.Printf("part 1: %d\n", day14.Part1(string(data)))
|
fmt.Printf("part 1: %d\n", day08.Part1(string(data)))
|
||||||
fmt.Printf("part 2: %d\n", day14.Part2(string(data)))
|
fmt.Printf("part 2: %d\n", day08.Part2(string(data)))
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
fmt.Printf("Execution time: %s\n", elapsed)
|
fmt.Printf("Execution time: %s\n", elapsed)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user