From 305ce52a983b9a956f88875b6bb87aed2ddf8d75 Mon Sep 17 00:00:00 2001 From: Gareth Date: Fri, 10 Jan 2025 20:44:19 +0000 Subject: [PATCH] Day08 --- 2024/gareth/day08/day08.go | 80 +++++++++++++++++++++++++++++++++ 2024/gareth/day08/day08_test.go | 39 ++++++++++++++++ 2024/gareth/day08/input.txt | 50 +++++++++++++++++++++ 2024/gareth/main.go | 8 ++-- 4 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 2024/gareth/day08/day08.go create mode 100644 2024/gareth/day08/day08_test.go create mode 100644 2024/gareth/day08/input.txt diff --git a/2024/gareth/day08/day08.go b/2024/gareth/day08/day08.go new file mode 100644 index 0000000..ce5a1cd --- /dev/null +++ b/2024/gareth/day08/day08.go @@ -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) +} diff --git a/2024/gareth/day08/day08_test.go b/2024/gareth/day08/day08_test.go new file mode 100644 index 0000000..f744f8f --- /dev/null +++ b/2024/gareth/day08/day08_test.go @@ -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) +} diff --git a/2024/gareth/day08/input.txt b/2024/gareth/day08/input.txt new file mode 100644 index 0000000..d4fb9a8 --- /dev/null +++ b/2024/gareth/day08/input.txt @@ -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 \ No newline at end of file diff --git a/2024/gareth/main.go b/2024/gareth/main.go index 04764d0..2004639 100644 --- a/2024/gareth/main.go +++ b/2024/gareth/main.go @@ -1,7 +1,7 @@ package main import ( - "aoc2024/day14" + "aoc2024/day08" "fmt" "os" "time" @@ -9,9 +9,9 @@ import ( func main() { start := time.Now() - data, _ := os.ReadFile("day14/input.txt") - fmt.Printf("part 1: %d\n", day14.Part1(string(data))) - fmt.Printf("part 2: %d\n", day14.Part2(string(data))) + data, _ := os.ReadFile("day08/input.txt") + fmt.Printf("part 1: %d\n", day08.Part1(string(data))) + fmt.Printf("part 2: %d\n", day08.Part2(string(data))) elapsed := time.Since(start) fmt.Printf("Execution time: %s\n", elapsed) }