days
This commit is contained in:
168
2024/go/day08/day08.go
Normal file
168
2024/go/day08/day08.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package day08
|
||||
|
||||
import (
|
||||
"adventofcode2024/utils/grid2d"
|
||||
"adventofcode2024/utils/inputs"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type loc struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
type pt struct {
|
||||
display rune
|
||||
antinodes int
|
||||
paired map[loc]pt
|
||||
visited map[rune]bool
|
||||
}
|
||||
|
||||
func Part1(input string) int {
|
||||
grid := inputs.ToGrid2D(input, "\n", "", pt{display: '.'}, func(c string) pt { return pt{display: rune(c[0]), paired: make(map[loc]pt), visited: make(map[rune]bool)} })
|
||||
for x1 := 0; x1 < grid.SizeX(); x1++ {
|
||||
for y1 := 0; y1 < grid.SizeY(); y1++ {
|
||||
pt1 := grid.Get(x1, y1)
|
||||
if pt1.display != '.' {
|
||||
for x2 := 0; x2 < grid.SizeX(); x2++ {
|
||||
for y2 := 0; y2 < grid.SizeY(); y2++ {
|
||||
pt2 := grid.Get(x2, y2)
|
||||
if x1 != x2 && y1 != y2 && pt1.display == pt2.display && !isPaired(x1, y1, pt1, pt2) {
|
||||
pt1.paired[loc{x: x2, y: y2}] = pt2
|
||||
pt2.paired[loc{x: x1, y: y1}] = pt1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for x := 0; x < grid.SizeX(); x++ {
|
||||
for y := 0; y < grid.SizeY(); y++ {
|
||||
pt1 := grid.Get(x, y)
|
||||
if pt1.display != '.' {
|
||||
for ploc := range pt1.paired {
|
||||
addAntiNodes(x, y, ploc.x, ploc.y, grid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
antinodes := 0
|
||||
for x := 0; x < grid.SizeX(); x++ {
|
||||
for y := 0; y < grid.SizeY(); y++ {
|
||||
pt := grid.Get(x, y)
|
||||
if pt.antinodes > 0 {
|
||||
antinodes++
|
||||
}
|
||||
}
|
||||
}
|
||||
return antinodes
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
grid := inputs.ToGrid2D(input, "\n", "", pt{display: '.'}, func(c string) pt { return pt{display: rune(c[0]), paired: make(map[loc]pt), visited: make(map[rune]bool)}})
|
||||
for x1 := 0; x1 < grid.SizeX(); x1++ {
|
||||
for y1 := 0; y1 < grid.SizeY(); y1++ {
|
||||
pt1 := grid.Get(x1, y1)
|
||||
if pt1.display != '.' {
|
||||
for x2 := 0; x2 < grid.SizeX(); x2++ {
|
||||
for y2 := 0; y2 < grid.SizeY(); y2++ {
|
||||
pt2 := grid.Get(x2, y2)
|
||||
if x1 != x2 && y1 != y2 && pt1.display == pt2.display && !isPaired(x1, y1, pt1, pt2) {
|
||||
pt1.paired[loc{x: x2, y: y2}] = pt2
|
||||
pt2.paired[loc{x: x1, y: y1}] = pt1
|
||||
pt1.antinodes++
|
||||
pt2.antinodes++
|
||||
grid.Set(x1, y1, pt1)
|
||||
grid.Set(x2, y2, pt2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for x := 0; x < grid.SizeX(); x++ {
|
||||
for y := 0; y < grid.SizeY(); y++ {
|
||||
pt1 := grid.Get(x, y)
|
||||
if pt1.display != '.' {
|
||||
for ploc := range pt1.paired {
|
||||
addAntiNodes_p2(pt1.display, x, y, ploc.x, ploc.y, grid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
antinodes := 0
|
||||
fmt.Printf("%v\n", grid.StringWithFormatter(formatter))
|
||||
for x := 0; x < grid.SizeX(); x++ {
|
||||
for y := 0; y < grid.SizeY(); y++ {
|
||||
pt := grid.Get(x, y)
|
||||
if pt.antinodes > 0 {
|
||||
antinodes++
|
||||
} else {
|
||||
if pt.display != '.' { antinodes++ }
|
||||
}
|
||||
}
|
||||
}
|
||||
return antinodes
|
||||
}
|
||||
|
||||
func formatter(p pt, x int, y int) string {
|
||||
if p.antinodes > 0 {
|
||||
return "#"
|
||||
}
|
||||
return "."
|
||||
}
|
||||
|
||||
func isPaired(x int, y int, p1 pt, pt2 pt) bool {
|
||||
return p1.paired[loc{x: x, y: y}].display == pt2.display
|
||||
}
|
||||
|
||||
func addAntiNodes(x1 int, y1 int, x2 int, y2 int, grid *grid2d.Grid[pt]) {
|
||||
px1, py1, px2, py2 := offsetPoints(x1, y1, x2, y2)
|
||||
|
||||
if px1 < 0 || px1 >= grid.SizeX() || py1 < 0 || py1 >= grid.SizeY() {
|
||||
return
|
||||
}
|
||||
pt1 := grid.Get(px1, py1)
|
||||
pt1.antinodes++
|
||||
grid.Set(px1, py1, pt1)
|
||||
if px2 < 0 || px2 >= grid.SizeX() || py2 < 0 || py2 >= grid.SizeY() {
|
||||
return
|
||||
}
|
||||
pt2 := grid.Get(px2, py2)
|
||||
pt2.antinodes++
|
||||
grid.Set(px2, py2, pt2)
|
||||
}
|
||||
|
||||
func addAntiNodes_p2(frequency rune, x1 int, y1 int, x2 int, y2 int, grid *grid2d.Grid[pt]) {
|
||||
px1, py1, px2, py2 := offsetPoints(x1, y1, x2, y2)
|
||||
if !(px1 < 0 || px1 >= grid.SizeX() || py1 < 0 || py1 >= grid.SizeY()) {
|
||||
pt1 := grid.Get(px1, py1)
|
||||
if !pt1.visited[frequency] {
|
||||
pt1.antinodes++
|
||||
pt1.visited[frequency] = true
|
||||
grid.Set(px1, py1, pt1)
|
||||
addAntiNodes_p2(frequency, x1, y1, px1, py1, grid)
|
||||
}
|
||||
}
|
||||
if !(px2 < 0 || px2 >= grid.SizeX() || py2 < 0 || py2 >= grid.SizeY()) {
|
||||
pt2 := grid.Get(px2, py2)
|
||||
if !pt2.visited[frequency] {
|
||||
pt2.antinodes++
|
||||
pt2.visited[frequency] = true
|
||||
grid.Set(px2, py2, pt2)
|
||||
addAntiNodes_p2(frequency, x2, y2, px2, py2, grid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Function to calculate two points symmetrically placed above and below the line
|
||||
func offsetPoints(x1, y1, x2, y2 int) (int, int, int, int) {
|
||||
dx := x1 - x2
|
||||
dy := y1 - y2
|
||||
|
||||
p1x := x1 + dx
|
||||
p1y := y1 + dy
|
||||
p2x := x2 - dx
|
||||
p2y := y2 - dy
|
||||
return p1x, p1y, p2x, p2y
|
||||
}
|
||||
39
2024/go/day08/day08_test.go
Normal file
39
2024/go/day08/day08_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package day08
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(`............
|
||||
........0...
|
||||
.....0......
|
||||
.......0....
|
||||
....0.......
|
||||
......A.....
|
||||
............
|
||||
............
|
||||
........A...
|
||||
.........A..
|
||||
............
|
||||
............`)
|
||||
require.Equal(t, 14, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(`............
|
||||
........0...
|
||||
.....0......
|
||||
.......0....
|
||||
....0.......
|
||||
......A.....
|
||||
............
|
||||
............
|
||||
........A...
|
||||
.........A..
|
||||
............
|
||||
............`)
|
||||
require.Equal(t, 34, r)
|
||||
}
|
||||
50
2024/go/day08/input.txt
Normal file
50
2024/go/day08/input.txt
Normal file
@@ -0,0 +1,50 @@
|
||||
.....y..........................p................r
|
||||
........I.........................................
|
||||
......................4.s.........................
|
||||
..........4.......................................
|
||||
....y.............................................
|
||||
......................................p.........r.
|
||||
..........0..s......N..................1.....p....
|
||||
..y........4.......................p..............
|
||||
...............0..................................
|
||||
..............0....t....N....h....................
|
||||
.............N....................................
|
||||
......j...................s............H...l..O...
|
||||
..........q.................H................O....
|
||||
..f...e.qj.....y...0..............................
|
||||
...........t..........................k..Q..r.....
|
||||
.........6................Q..s...x......W.........
|
||||
....2..b...e....t..4.........c.....xW.j...........
|
||||
...e....................w................1.....O..
|
||||
..e..j..5...........................c.............
|
||||
.........B..2...............MK................H...
|
||||
...2......b...g..X...q..........h...............O.
|
||||
...q...2..........m....k...i...............QV.x...
|
||||
...................i.........W.k.............HQ...
|
||||
........b...X...............D..........c...N......
|
||||
................................l..........h.....I
|
||||
.m...........g......l.......c.............3......V
|
||||
....X.......m........g...V.K...7......F.d.........
|
||||
.........b.X...U..........................C.......
|
||||
.....................l..............o.1....C......
|
||||
............u.............K..............3...d....
|
||||
......................i.T....f................V...
|
||||
..............................1.k.................
|
||||
.B.....E......9..m....K..5.M......................
|
||||
...P...............M...95....o..i........I........
|
||||
...............................S......3......wI...
|
||||
.....EP...........9........5..T.R.................
|
||||
.P..........v..9......f.............R.Co..w3......
|
||||
..........h...SG..v.E...7..f.T....................
|
||||
..........6..........L.................Y.......d..
|
||||
..........B...............U........D..............
|
||||
....B................U.....8..M....n...J..........
|
||||
.........................L................Fw......
|
||||
....L6E.P.................7.UG....J.....Y.D.......
|
||||
........t........v...SJ........n..d...............
|
||||
......................8v.....uG...................
|
||||
..................L.....n.........................
|
||||
...............T..............n......D............
|
||||
..............o.........8................J.Y.R....
|
||||
..................S...............u....F.......R..
|
||||
........6..............u.....7.8..........Y..F....
|
||||
Reference in New Issue
Block a user