Finished day 8
This commit is contained in:
parent
302185bbdf
commit
4e3d8d5562
2 changed files with 171 additions and 0 deletions
121
2024/day8/main.go
Normal file
121
2024/day8/main.go
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Point struct {
|
||||
x, y int
|
||||
}
|
||||
|
||||
func isValid(p Point, rows, cols int) bool {
|
||||
return p.x >= 0 && p.x < rows && p.y >= 0 && p.y < cols
|
||||
}
|
||||
|
||||
func Part1(grid map[rune][]Point, rows, cols int) int {
|
||||
antinodes := make(map[Point]bool)
|
||||
|
||||
for _, antennas := range grid {
|
||||
if len(antennas) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
for i := 0; i < len(antennas); i++ {
|
||||
for j := i + 1; j < len(antennas); j++ {
|
||||
a1, a2 := antennas[i], antennas[j]
|
||||
dx, dy := a2.x-a1.x, a2.y-a1.y
|
||||
|
||||
forward := Point{a1.x - dx, a1.y - dy}
|
||||
if isValid(forward, rows, cols) {
|
||||
antinodes[forward] = true
|
||||
}
|
||||
|
||||
backward := Point{a2.x + dx, a2.y + dy}
|
||||
if isValid(backward, rows, cols) {
|
||||
antinodes[backward] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return len(antinodes)
|
||||
}
|
||||
|
||||
func Part2(grid map[rune][]Point, rows, cols int) int {
|
||||
antinodes := make(map[Point]bool)
|
||||
for _, antennas := range grid {
|
||||
if len(antennas) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
for i := 0; i < len(antennas); i++ {
|
||||
for j := i + 1; j < len(antennas); j++ {
|
||||
a1, a2 := antennas[i], antennas[j]
|
||||
|
||||
antinodes[a1] = true
|
||||
antinodes[a2] = true
|
||||
|
||||
dx, dy := a2.x-a1.x, a2.y-a1.y
|
||||
|
||||
forward := Point{a1.x - dx, a1.y - dy}
|
||||
for isValid(forward, rows, cols) {
|
||||
antinodes[forward] = true
|
||||
forward = Point{forward.x - dx, forward.y - dy}
|
||||
}
|
||||
|
||||
backward := Point{a2.x + dx, a2.y + dy}
|
||||
for isValid(backward, rows, cols) {
|
||||
antinodes[backward] = true
|
||||
backward = Point{backward.x + dx, backward.y + dy}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return len(antinodes)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if len(flag.Args()) == 0 {
|
||||
panic("Please provide a filename")
|
||||
}
|
||||
|
||||
filename := flag.Args()[0]
|
||||
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
grid := make(map[rune][]Point)
|
||||
scanner := bufio.NewScanner(file)
|
||||
rows, cols := 0, 0
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
for c, ch := range line {
|
||||
if ch != '.' {
|
||||
grid[ch] = append(grid[ch], Point{rows, c})
|
||||
}
|
||||
}
|
||||
|
||||
cols = len(line)
|
||||
rows++
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
answer := Part1(grid, rows, cols)
|
||||
fmt.Println(answer)
|
||||
|
||||
answer = Part2(grid, rows, cols)
|
||||
fmt.Println(answer)
|
||||
}
|
||||
50
2024/inputs/day8.txt
Normal file
50
2024/inputs/day8.txt
Normal file
|
|
@ -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.....
|
||||
Loading…
Add table
Add a link
Reference in a new issue