This commit is contained in:
2024-12-08 14:54:38 +01:00
parent ee0afed6da
commit 87c69c2b4c
3 changed files with 274 additions and 0 deletions

109
day8/antinode.go Normal file
View File

@@ -0,0 +1,109 @@
package main
import (
"os"
"fmt"
"strings"
)
// data types
type Point struct {
x int16
y int16
}
type Antenna struct {
point Point
freq byte
}
// general util
func check(e error) {
if e != nil { panic(e) }
}
func (p Point) Mul(scalar int) Point {
return Point { x: p.x * int16(scalar), y: p.y * int16(scalar) }
}
func (p Point) Add(other Point) Point {
return Point { x: p.x + other.x, y: p.y + other.y }
}
func (p Point) Sub(other Point) Point {
return Point { x: p.x - other.x, y: p.y - other.y }
}
func contains(arr []Point, p Point) bool {
for _, elem := range arr {
if elem == p { return true }
}
return false
}
func addNonDuplicate(arr []Point, p Point) []Point {
if contains(arr, p) { return arr
} else { return append(arr, p) }
}
func newAntennas(data string) []Antenna {
/* Reads the puzzle input, return a list of antenna structs.
*/
antennas := make([]Antenna, 0, 100)
for y, line := range strings.Split(data, "\n") {
for x, char := range []byte(line) {
if char != '.' {
antennas = append(antennas, Antenna{ point: Point{x: int16(x), y: int16(y)}, freq: char })
}
}
}
return antennas
}
func antinodes(points [2]Point) [2]Point {
var nodes [2]Point
distance := points[0].Sub(points[1])
nodes[0] = points[0].Add(distance)
nodes[1] = points[1].Sub(distance)
return nodes
}
// main loop
func main() {
// read in file
dat, err := os.ReadFile("data.txt")
check(err)
dat_str := strings.TrimSpace(string(dat))
lines := strings.Split(dat_str, "\n")
height, width := int16(len(lines)), int16(len(lines[0]))
antennas := newAntennas(dat_str)
pairs := make([][2]Point, 0)
for _, ant1 := range antennas {
for _, ant2 := range antennas {
if ant1.freq == ant2.freq && ant1.point != ant2.point {
pairs = append(pairs, [2]Point{ant1.point, ant2.point})
}
}
}
nodes := make([]Point, 0, len(pairs) * 2)
for _, pair := range pairs {
ns := antinodes(pair)
if ns[0].x >= 0 && ns[0].x < width && ns[0].y >= 0 && ns[0].y < height {
nodes = addNonDuplicate(nodes, ns[0])
}
if ns[1].x >= 0 && ns[1].x < width && ns[1].y >= 0 && ns[1].y < height {
nodes = addNonDuplicate(nodes, ns[1])
}
}
fmt.Println(len(nodes))
}

51
day8/data.txt Normal file
View File

@@ -0,0 +1,51 @@
.A...........5........................pL..........
.................................p......L.........
......................................L...........
.......................................C..........
........v...................7...............C.....
..................................p........L......
.................vA......3........................
.......A.....3....................................
........................s....X3...................
..A......5.................9....3.................
.......8...........s.........7.............C..m...
................8......t........7.......9.........
....................o......Z.............y........
...............s.......Y.v.o......y....0..........
..................................................
..5................8.......................m...J..
5...............................0....aX...........
.V............v.s.........Z.o..7....a.............
2..........f...........P..............9...J.M.....
...............f..........P.....V......y....1.J...
...g...................o.......0l...........N..B..
..................Y...............................
......G...............f.....Z..t..............1...
............G......Z......h................B....C.
.........w....h.Y....j............a........J..y...
.............P....z..........................1....
w.......P...z...R......r8.........................
........w.........................................
.................h.G.........m............BM......
......4.....fa.................G...i....X......W..
V........4..............................tW.9...i..
............2h..............0.......tX...M........
.....z.........................l..................
.......2..........................................
..r........................Y................W...i.
.......w.........q..................i.............
.........H.2....4.................................
..........Q.....j.......M.....lrN.................
..x...H.Q.......O.....c...........................
....H.......................S.....................
.....................O..S.......6..........b......
...c.......F...Q.j.........l....T.....R...........
...........Q.F.......c.I.....1.........R....T.....
............F........I.O......r..T.............b..
..n.........q.........F.I..............T..b.......
.......n...........z..O....x.......N........b.....
.....S............................................
..........q.........cS..x4I......6................
..j.....gn.q.......x...................N...6......
...........g..n................R......B...........

114
day8/harmonic.go Normal file
View File

@@ -0,0 +1,114 @@
package main
import (
"os"
"fmt"
"strings"
)
// data types
type Point struct {
x int16
y int16
}
type Antenna struct {
point Point
freq byte
}
// general util
func check(e error) {
if e != nil { panic(e) }
}
func (p Point) Mul(scalar int) Point {
return Point { x: p.x * int16(scalar), y: p.y * int16(scalar) }
}
func (p Point) Add(other Point) Point {
return Point { x: p.x + other.x, y: p.y + other.y }
}
func (p Point) Sub(other Point) Point {
return Point { x: p.x - other.x, y: p.y - other.y }
}
func contains(arr []Point, p Point) bool {
for _, elem := range arr {
if elem == p { return true }
}
return false
}
func addNonDuplicate(arr []Point, p Point) []Point {
if contains(arr, p) { return arr
} else { return append(arr, p) }
}
func newAntennas(data string) []Antenna {
/* Reads the puzzle input, return a list of antenna structs.
*/
antennas := make([]Antenna, 0, 100)
for y, line := range strings.Split(data, "\n") {
for x, char := range []byte(line) {
if char != '.' {
antennas = append(antennas, Antenna{ point: Point{x: int16(x), y: int16(y)}, freq: char })
}
}
}
return antennas
}
func antinodes(points [2]Point, n int) []Point {
/* len(antinodes(..., n)) == 2*n + 2 */
nodes := make([]Point, 2*n)
distance := points[0].Sub(points[1])
for i := 0; i < n; i++ {
nodes[i] = points[0].Add(distance.Mul(i + 1))
nodes[i + n] = points[1].Sub(distance.Mul(i + 1))
}
nodes = append(nodes, points[0])
nodes = append(nodes, points[1])
return nodes
}
// main loop
func main() {
// read in file
dat, err := os.ReadFile("data.txt")
check(err)
dat_str := strings.TrimSpace(string(dat))
lines := strings.Split(dat_str, "\n")
height, width := int16(len(lines)), int16(len(lines[0]))
antennas := newAntennas(dat_str)
pairs := make([][2]Point, 0)
for _, ant1 := range antennas {
for _, ant2 := range antennas {
if ant1.freq == ant2.freq && ant1.point != ant2.point {
pairs = append(pairs, [2]Point{ant1.point, ant2.point})
}
}
}
nodes := make([]Point, 0, len(pairs) * 2)
for _, pair := range pairs {
ns := antinodes(pair, 100)
for _, n := range ns {
if n.x >= 0 && n.x < width && n.y >= 0 && n.y < height {
nodes = addNonDuplicate(nodes, n)
}
}
}
fmt.Println(len(nodes))
}