From 4fc651a2974bb2ac86256e6988b4364b5282ed9b Mon Sep 17 00:00:00 2001 From: SebastianStork Date: Fri, 17 Jan 2025 10:36:01 +0100 Subject: [PATCH] Solve 2024 day 8 in go --- 2024/go/day-08/go.mod | 3 + 2024/go/day-08/input | 50 ++++++++++++++++ 2024/go/day-08/main.go | 133 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 2024/go/day-08/go.mod create mode 100644 2024/go/day-08/input create mode 100644 2024/go/day-08/main.go diff --git a/2024/go/day-08/go.mod b/2024/go/day-08/go.mod new file mode 100644 index 0000000..2fc5458 --- /dev/null +++ b/2024/go/day-08/go.mod @@ -0,0 +1,3 @@ +module day-08 + +go 1.23 diff --git a/2024/go/day-08/input b/2024/go/day-08/input new file mode 100644 index 0000000..5189c13 --- /dev/null +++ b/2024/go/day-08/input @@ -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 diff --git a/2024/go/day-08/main.go b/2024/go/day-08/main.go new file mode 100644 index 0000000..106ab4a --- /dev/null +++ b/2024/go/day-08/main.go @@ -0,0 +1,133 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" +) + +type position struct { + row, col int +} + +func readInput() (antennas map[rune][]position, dimensions position, err error) { + content, err := os.ReadFile("input") + if err != nil { + return + } + + lines := strings.Split(strings.TrimSpace(string(content)), "\n") + antennas = make(map[rune][]position) + + for rowIndex, line := range lines { + for colIndex, frequency := range line { + if frequency != '.' { + antennas[frequency] = append(antennas[frequency], position{row: rowIndex, col: colIndex}) + } + } + } + + dimensions = position{row: len(lines), col: len(lines[0])} + + return +} + +func isInBounds(antinode, dimensions position) bool { + return (antinode.row >= 0 && antinode.row < dimensions.row) && (antinode.col >= 0 && antinode.col < dimensions.col) +} + +func deltaVector(antenna1, antenna2 position) position { + return position{ + row: antenna2.row - antenna1.row, + col: antenna2.col - antenna1.col, + } +} + +func antennaPairAntinodes(antenna1, antenna2, dimensions position) []position { + deltaVector := deltaVector(antenna1, antenna2) + + antinode1 := position{ + row: antenna1.row - deltaVector.row, + col: antenna1.col - deltaVector.col, + } + antinode2 := position{ + row: antenna2.row + deltaVector.row, + col: antenna2.col + deltaVector.col, + } + + var antinodes []position + if isInBounds(antinode1, dimensions) { + antinodes = append(antinodes, antinode1) + } + if isInBounds(antinode2, dimensions) { + antinodes = append(antinodes, antinode2) + } + + return antinodes +} + +func antennaPairResonantAntinodes(antenna1, antenna2, dimensions position) []position { + var resonantAntinodes []position + deltaVector := deltaVector(antenna1, antenna2) + + // Resonant antinodes + antinode1 := antenna1 + for isInBounds(antinode1, dimensions) { + resonantAntinodes = append(resonantAntinodes, antinode1) + + antinode1.row -= deltaVector.row + antinode1.col -= deltaVector.col + } + + antinode2 := antenna2 + for isInBounds(antinode2, dimensions) { + resonantAntinodes = append(resonantAntinodes, antinode2) + + antinode2.row += deltaVector.row + antinode2.col += deltaVector.col + } + + return resonantAntinodes +} + +func findAllAntinodes(antennas map[rune][]position, dimensions position) (uniqueAntinodes, uniqueResonantAntinodes map[position]struct{}) { + uniqueAntinodes = make(map[position]struct{}) + uniqueResonantAntinodes = make(map[position]struct{}) + + for _, frequencyGroup := range antennas { + for antenna1Index := 0; antenna1Index < len(frequencyGroup)-1; antenna1Index++ { + for antenna2Index := antenna1Index + 1; antenna2Index < len(frequencyGroup); antenna2Index++ { + antenna1 := frequencyGroup[antenna1Index] + antenna2 := frequencyGroup[antenna2Index] + + foundAntinodes := antennaPairAntinodes(antenna1, antenna2, dimensions) + foundResonantAntinodes := antennaPairResonantAntinodes(antenna1, antenna2, dimensions) + + for _, antinode := range foundAntinodes { + uniqueAntinodes[antinode] = struct{}{} + } + + for _, antinode := range foundResonantAntinodes { + uniqueResonantAntinodes[antinode] = struct{}{} + } + } + } + } + + return +} + +func main() { + antennas, dimensions, err := readInput() + if err != nil { + log.Fatalln(err) + } + + uniqueAntinodes, uniqueResonantAntinodes := findAllAntinodes(antennas, dimensions) + + // Part one + fmt.Println("Number of unique antinodes:", len(uniqueAntinodes)) + // Part two + fmt.Println("Number of unique antinodes when taking resonant harmonics into account:", len(uniqueResonantAntinodes)) +}