Compare commits

..

No commits in common. "1c60c034e3040a939ae25babc6830161843c3bb1" and "df605fba1516f688c6a848931d687eabaf2c57b4" have entirely different histories.

8 changed files with 39 additions and 27 deletions

View File

@ -2,14 +2,14 @@ goos: linux
goarch: amd64 goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-06 pkg: github.com/onyx-and-iris/aoc2024/day-06
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1 1787027305 ns/op BenchmarkSolve-12 1 1815042506 ns/op
BenchmarkSolve-12 1 1669661404 ns/op BenchmarkSolve-12 1 1670139306 ns/op
BenchmarkSolve-12 1 1702069604 ns/op BenchmarkSolve-12 1 1708459605 ns/op
BenchmarkSolve-12 1 1702577205 ns/op BenchmarkSolve-12 1 1687274507 ns/op
BenchmarkSolve-12 1 1708872905 ns/op BenchmarkSolve-12 1 1722070212 ns/op
BenchmarkSolve-12 1 1825434105 ns/op BenchmarkSolve-12 1 1728997012 ns/op
BenchmarkSolve-12 1 1667198605 ns/op BenchmarkSolve-12 1 1738883012 ns/op
BenchmarkSolve-12 1 1650618104 ns/op BenchmarkSolve-12 1 1686811712 ns/op
BenchmarkSolve-12 1 1726260907 ns/op BenchmarkSolve-12 1 1701938016 ns/op
BenchmarkSolve-12 1 1667980407 ns/op BenchmarkSolve-12 1 1701384522 ns/op
ok github.com/onyx-and-iris/aoc2024/day-06 17.133s ok github.com/onyx-and-iris/aoc2024/day-06 17.190s

View File

@ -2,5 +2,5 @@ goos: linux
goarch: amd64 goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-06/internal/one pkg: github.com/onyx-and-iris/aoc2024/day-06/internal/one
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.006992 ns/op BenchmarkSolve-12 1000000000 0.007368 ns/op
ok github.com/onyx-and-iris/aoc2024/day-06/internal/one 0.052s ok github.com/onyx-and-iris/aoc2024/day-06/internal/one 0.052s

View File

@ -7,8 +7,8 @@ import (
) )
type graph struct { type graph struct {
startPoint point
data []string data []string
startPoint point
} }
func newGraph() *graph { func newGraph() *graph {

View File

@ -2,5 +2,5 @@ goos: linux
goarch: amd64 goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-06/internal/two pkg: github.com/onyx-and-iris/aoc2024/day-06/internal/two
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.0000354 ns/op BenchmarkSolve-12 1000000000 0.0000547 ns/op
ok github.com/onyx-and-iris/aoc2024/day-06/internal/two 0.006s ok github.com/onyx-and-iris/aoc2024/day-06/internal/two 0.006s

View File

@ -5,4 +5,5 @@ const (
E E
S S
W W
obstacle
) )

View File

@ -7,12 +7,13 @@ import (
) )
type graph struct { type graph struct {
startPoint point
data []string data []string
startPoint point
obstacles []point
} }
func newGraph(startPoint point, data []string) *graph { func newGraph() *graph {
return &graph{startPoint: startPoint, data: data} return &graph{}
} }
func (g *graph) String() string { func (g *graph) String() string {

View File

@ -24,7 +24,9 @@ func Solve(buf []byte) (int, error) {
continue continue
} }
g := newGraph(graph.startPoint, slices.Clone(graph.data)) g := newGraph()
g.startPoint = graph.startPoint
g.data = slices.Clone(graph.data)
g.data[loc.Y] = replaceAtIndex(g.data[loc.Y], 'O', loc.X) g.data[loc.Y] = replaceAtIndex(g.data[loc.Y], 'O', loc.X)
p := g.startPoint p := g.startPoint
@ -35,18 +37,19 @@ func Solve(buf []byte) (int, error) {
p.y == 0 && p.direction == N || p.y == 0 && p.direction == N ||
p.y == len(g.data)-1 && p.direction == S || p.y == len(g.data)-1 && p.direction == S ||
p.x == len(g.data[p.y])-1 && p.direction == E) { p.x == len(g.data[p.y])-1 && p.direction == E) {
_, ok := visited[p]
if ok {
log.Tracef("loop: \n%s\n", g.trace(visited))
isLoop <- true
return
}
visited[p] = struct{}{}
p = nextPoint(p) p = nextPoint(p)
if g.valueAt(p.x, p.y) == '#' || g.valueAt(p.x, p.y) == 'O' { if g.valueAt(p.x, p.y) == '#' || g.valueAt(p.x, p.y) == 'O' {
p.recalibrate() p.recalibrate()
} }
_, ok := visited[p]
if !ok {
visited[p] = struct{}{}
} else {
log.Tracef("loop: \n%s\n", g.trace(visited))
isLoop <- true
return
}
} }
isLoop <- false isLoop <- false
}() }()

View File

@ -7,7 +7,7 @@ import (
) )
func parseLines(r io.Reader) (*graph, error) { func parseLines(r io.Reader) (*graph, error) {
graph := &graph{} graph := newGraph()
count := 0 count := 0
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
@ -18,6 +18,13 @@ func parseLines(r io.Reader) (*graph, error) {
graph.startPoint = point{indx, count, N} graph.startPoint = point{indx, count, N}
} }
graph.data = append(graph.data, scanner.Text()) graph.data = append(graph.data, scanner.Text())
for i, r := range line {
if r == '#' {
graph.obstacles = append(graph.obstacles, point{i, count, obstacle})
}
}
count++ count++
} }