From 295e4fd0315e4eea92c7278e9f3d60323764c5f6 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sun, 8 Dec 2024 16:56:51 +0100 Subject: Day 8 --- day8/data.txt | 50 ++++++++++++++++++++++ day8/example.txt | 12 ++++++ day8/go.mod | 3 ++ day8/main.go | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.work | 1 + 5 files changed, 192 insertions(+) create mode 100644 day8/data.txt create mode 100644 day8/example.txt create mode 100644 day8/go.mod create mode 100644 day8/main.go diff --git a/day8/data.txt b/day8/data.txt new file mode 100644 index 0000000..3cefaeb --- /dev/null +++ b/day8/data.txt @@ -0,0 +1,50 @@ +.....wV....q.....................................n +.......w......q.h.....Vn.........................D +............w.S..G.....................DT......... +......S........h......e..T.....y......D........... +......m.......Ae.......T........o................. +....m....S........................................ +...m..........................n........8.......... +.........2...G......................n............. +..2........V.......h................Q............. +............................o..................... +.Z......I..U....e...u.....G....o.................. +...N..G.........................................y. +.....I............q.......h...................s... +......U........qI....o.V..Rz........8........k.... +......d.Z.........................R.......8y...... +.........e..............T.....l................... +.......2.........................u...R............ +.....d.............................Q.............. +...................v.....................s.Q....M. +........2..........4.....................8..7.k... +...........x..N..................A..........k..... +...........ZN...........v...............K......... +...d.......N.....................Ky.6............. +...........................l6..................... +....L....g.................4.......k..K.......0... +..............L...........4R................s..... +U......r..............H.4......................... +.......U.............a.......H.............u...... +......xY...............l.......................... +...................................6..u........... +........Y......L......l............0.............. +......9..L...........A.....v..HEa........K........ +..................v........6.EX.............z..... +d..Y.............m......A......................... +......................a.i......M...........z...... +...................g.......................0...... +...............................H.........i........ +..........3................W........E...i...0..... +.................t.a....g.................5....... +.r...t...........................7.....5.......... +....................................7....5........ +....................g.Y...wMz..................... +9..........O....3................W.7..E..XD...1... +t..............3.x.....9..........W.M............. +...9............W................................. +Z.............x................X.i......5......... +...........3.....................................1 +...................O.......s....X................. +..............r................................... +..........................O.................1..... diff --git a/day8/example.txt b/day8/example.txt new file mode 100644 index 0000000..79e3286 --- /dev/null +++ b/day8/example.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ diff --git a/day8/go.mod b/day8/go.mod new file mode 100644 index 0000000..5baa592 --- /dev/null +++ b/day8/go.mod @@ -0,0 +1,3 @@ +module stevenlr.com/aoc2024/day8 + +go 1.22.2 diff --git a/day8/main.go b/day8/main.go new file mode 100644 index 0000000..11b0d09 --- /dev/null +++ b/day8/main.go @@ -0,0 +1,126 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +type Position struct { + X, Y int +} + +func ByteToAntenna(b byte) int { + if b >= '0' && b <= '9' { + return int(b - '0') + } else if b >= 'a' && b <= 'z' { + return int(b - 'a' + 10) + } else if b >= 'A' && b <= 'Z' { + return int(b - 'A' + 36) + } else { + return -1 + } +} + +func IsInMap(p Position, w, h int) bool { + if p.X < 0 || p.X >= w { + return false + } + if p.Y < 0 || p.Y >= h { + return false + } + return true +} + +func ComputeAntinodes(a, b Position, w, h int, first, repeats int) (antinodes []Position) { + dx := b.X - a.X + dy := b.Y - a.Y + + for i := first; i <= repeats; i++ { + n1, n2 := Position{a.X - dx*i, a.Y - dy*i}, Position{b.X + dx*i, b.Y + dy*i} + n1InMap := IsInMap(n1, w, h) + n2InMap := IsInMap(n2, w, h) + + if n1InMap { + antinodes = append(antinodes, n1) + } + + if n2InMap { + antinodes = append(antinodes, n2) + } + + if !n1InMap && !n2InMap { + break + } + } + + return +} + +func main() { + { + width, height, antinodes := readData("example.txt") + fmt.Println(countAntinodes(width, height, antinodes, 1, 1)) + fmt.Println(countAntinodes(width, height, antinodes, 0, 999)) + } + { + width, height, antinodes := readData("data.txt") + fmt.Println(countAntinodes(width, height, antinodes, 1, 1)) + fmt.Println(countAntinodes(width, height, antinodes, 0, 999)) + } +} + +func countAntinodes(width, height int, antennas map[int][]Position, first, repeats int) (count int) { + marked := make([][]bool, height) + for y := 0; y < height; y++ { + marked[y] = make([]bool, width) + } + + for _, positions := range antennas { + for i := 0; i < len(positions)-1; i++ { + for j := i + 1; j < len(positions); j++ { + for _, p := range ComputeAntinodes(positions[i], positions[j], width, height, first, repeats) { + marked[p.Y][p.X] = true + } + } + } + } + + for y := 0; y < height; y++ { + for x := 0; x < len(marked[y]); x++ { + if marked[y][x] { + count++ + } + } + } + + return +} + +func readData(fileName string) (width, height int, antennas map[int][]Position) { + fp, err := os.Open(fileName) + if err != nil { + panic(err) + } + + scanner := bufio.NewScanner(fp) + antennas = make(map[int][]Position) + + y := 0 + for ; scanner.Scan(); y++ { + line := strings.TrimSpace(scanner.Text()) + width = len(line) + + for x := 0; x < width; x++ { + antenna := ByteToAntenna(line[x]) + if antenna >= 0 { + antennas[antenna] = append(antennas[antenna], Position{x, y}) + } + } + } + + height = y + + return +} diff --git a/go.work b/go.work index 6c04533..ebf24bc 100644 --- a/go.work +++ b/go.work @@ -8,4 +8,5 @@ use ( ./day5 ./day6 ./day7 + ./day8 ) -- cgit