remove internal/util

This commit is contained in:
onyx-and-iris 2025-01-07 17:24:01 +00:00
parent 2b675af081
commit 84c1013f2c
6 changed files with 20 additions and 20 deletions

View File

@ -2,8 +2,6 @@ package one
import ( import (
"strings" "strings"
"github.com/onyx-and-iris/aoc2024/day-08/internal/util"
) )
type graph struct { type graph struct {
@ -26,10 +24,10 @@ func (g *graph) isOutOfBounds(c coords) bool {
func (g *graph) debug() string { func (g *graph) debug() string {
for _, antenna := range g.antennae { for _, antenna := range g.antennae {
g.data[antenna.y] = util.ReplaceAtIndex(g.data[antenna.y], antenna.identifier, antenna.x) g.data[antenna.y] = replaceAtIndex(g.data[antenna.y], antenna.identifier, antenna.x)
} }
for antinode := range g.antinodes { for antinode := range g.antinodes {
g.data[antinode.y] = util.ReplaceAtIndex(g.data[antinode.y], '#', antinode.x) g.data[antinode.y] = replaceAtIndex(g.data[antinode.y], '#', antinode.x)
} }
return g.String() return g.String()

View File

@ -32,3 +32,9 @@ func parseLines(r io.Reader) (*graph, error) {
return graph, nil return graph, nil
} }
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}

View File

@ -2,8 +2,6 @@ package two
import ( import (
"strings" "strings"
"github.com/onyx-and-iris/aoc2024/day-08/internal/util"
) )
type graph struct { type graph struct {
@ -26,10 +24,10 @@ func (g *graph) isOutOfBounds(c coords) bool {
func (g *graph) debug() string { func (g *graph) debug() string {
for _, antenna := range g.antennae { for _, antenna := range g.antennae {
g.data[antenna.y] = util.ReplaceAtIndex(g.data[antenna.y], antenna.identifier, antenna.x) g.data[antenna.y] = replaceAtIndex(g.data[antenna.y], antenna.identifier, antenna.x)
} }
for antinode := range g.antinodes.data { for antinode := range g.antinodes.data {
g.data[antinode.y] = util.ReplaceAtIndex(g.data[antinode.y], '#', antinode.x) g.data[antinode.y] = replaceAtIndex(g.data[antinode.y], '#', antinode.x)
} }
return g.String() return g.String()

View File

@ -27,8 +27,7 @@ func Solve(buf []byte) (int, error) {
continue continue
} }
all := []coords{a.coords, b.coords} for _, coords := range calcAntiNodePos(a.coords, b.coords, []coords{a.coords, b.coords}, graph) {
for _, coords := range calcAntiNodePos(a.coords, b.coords, graph, all) {
if !graph.antinodes.contains(coords) { if !graph.antinodes.contains(coords) {
graph.antinodes.insert(coords) graph.antinodes.insert(coords)
} }
@ -44,7 +43,7 @@ func Solve(buf []byte) (int, error) {
return graph.antinodes.len(), nil return graph.antinodes.len(), nil
} }
func calcAntiNodePos(a, b coords, graph *graph, all []coords) []coords { func calcAntiNodePos(a, b coords, all []coords, g *graph) []coords {
xdiff := int(math.Abs(float64(a.x - b.x))) xdiff := int(math.Abs(float64(a.x - b.x)))
ydiff := int(math.Abs(float64(a.y - b.y))) ydiff := int(math.Abs(float64(a.y - b.y)))
@ -59,8 +58,8 @@ func calcAntiNodePos(a, b coords, graph *graph, all []coords) []coords {
next = newCoords(b.x-xdiff, b.y-ydiff) next = newCoords(b.x-xdiff, b.y-ydiff)
} }
if graph.isOutOfBounds(next) { if g.isOutOfBounds(next) {
return all return all
} }
return calcAntiNodePos(b, next, graph, append(all, next)) return calcAntiNodePos(b, next, append(all, next), g)
} }

View File

@ -32,3 +32,9 @@ func parseLines(r io.Reader) (*graph, error) {
return graph, nil return graph, nil
} }
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}

View File

@ -1,7 +0,0 @@
package util
func ReplaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}