mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-08 18:13:36 +00:00
add day-06 + benchmarks
This commit is contained in:
6
day-06/internal/two/benchmark
Normal file
6
day-06/internal/two/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-06/internal/two
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.0000547 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-06/internal/two 0.006s
|
||||
9
day-06/internal/two/direction.go
Normal file
9
day-06/internal/two/direction.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package two
|
||||
|
||||
const (
|
||||
N = iota
|
||||
E
|
||||
S
|
||||
W
|
||||
obstacle
|
||||
)
|
||||
35
day-06/internal/two/graph.go
Normal file
35
day-06/internal/two/graph.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-06/internal/util"
|
||||
)
|
||||
|
||||
type graph struct {
|
||||
data []string
|
||||
startPoint point
|
||||
obstacles []point
|
||||
}
|
||||
|
||||
func newGraph() *graph {
|
||||
return &graph{}
|
||||
}
|
||||
|
||||
func (g *graph) String() string {
|
||||
return strings.Join(g.data, "\n")
|
||||
}
|
||||
|
||||
func (g *graph) valueAt(x, y int) rune {
|
||||
return rune(g.data[y][x])
|
||||
}
|
||||
|
||||
func (g *graph) trace(visited map[point]struct{}) string {
|
||||
for loc := range visited {
|
||||
if !(rune(g.data[loc.y][loc.x]) == 'O') {
|
||||
g.data[loc.y] = util.ReplaceAtIndex(g.data[loc.y], '+', loc.x)
|
||||
}
|
||||
}
|
||||
|
||||
return g.String()
|
||||
}
|
||||
47
day-06/internal/two/point.go
Normal file
47
day-06/internal/two/point.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package two
|
||||
|
||||
import "fmt"
|
||||
|
||||
type point struct {
|
||||
x int
|
||||
y int
|
||||
direction int
|
||||
}
|
||||
|
||||
func (p *point) String() string {
|
||||
return fmt.Sprintf("X: %d Y:%d Direction: %s", p.x, p.y, []string{"N", "E", "S", "W", "Obs"}[p.direction])
|
||||
}
|
||||
|
||||
func (p *point) recalibrate() {
|
||||
switch p.direction {
|
||||
case N:
|
||||
p.y++
|
||||
case E:
|
||||
p.x--
|
||||
case S:
|
||||
p.y--
|
||||
case W:
|
||||
p.x++
|
||||
}
|
||||
|
||||
if p.direction == W {
|
||||
p.direction = 0
|
||||
} else {
|
||||
p.direction++
|
||||
}
|
||||
}
|
||||
|
||||
func nextPoint(current point) point {
|
||||
switch current.direction {
|
||||
case N:
|
||||
return point{current.x, current.y - 1, current.direction}
|
||||
case E:
|
||||
return point{current.x + 1, current.y, current.direction}
|
||||
case S:
|
||||
return point{current.x, current.y + 1, current.direction}
|
||||
case W:
|
||||
return point{current.x - 1, current.y, current.direction}
|
||||
default:
|
||||
return point{}
|
||||
}
|
||||
}
|
||||
67
day-06/internal/two/solve.go
Normal file
67
day-06/internal/two/solve.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"slices"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-06/internal/one"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
graph, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
conc := len(one.Visited) - 1
|
||||
isLoop := make(chan bool)
|
||||
|
||||
for loc := range one.Visited {
|
||||
if loc.X == graph.startPoint.x && loc.Y == graph.startPoint.y {
|
||||
continue
|
||||
}
|
||||
|
||||
g := newGraph()
|
||||
g.startPoint = graph.startPoint
|
||||
g.data = slices.Clone(graph.data)
|
||||
g.data[loc.Y] = replaceAtIndex(g.data[loc.Y], 'O', loc.X)
|
||||
|
||||
p := g.startPoint
|
||||
go func() {
|
||||
visited := make(map[point]struct{})
|
||||
|
||||
for !(p.x == 0 && p.direction == W ||
|
||||
p.y == 0 && p.direction == N ||
|
||||
p.y == len(g.data)-1 && p.direction == S ||
|
||||
p.x == len(g.data[p.y])-1 && p.direction == E) {
|
||||
p = nextPoint(p)
|
||||
if g.valueAt(p.x, p.y) == '#' || g.valueAt(p.x, p.y) == 'O' {
|
||||
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
|
||||
}()
|
||||
}
|
||||
|
||||
var numLoops int
|
||||
for range conc {
|
||||
res := <-isLoop
|
||||
if res {
|
||||
numLoops++
|
||||
}
|
||||
}
|
||||
|
||||
return numLoops, nil
|
||||
}
|
||||
15
day-06/internal/two/solve_internal_test.go
Normal file
15
day-06/internal/two/solve_internal_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed testdata/input.txt
|
||||
var data []byte
|
||||
|
||||
func BenchmarkSolve(b *testing.B) {
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
Solve(data)
|
||||
}
|
||||
42
day-06/internal/two/util.go
Normal file
42
day-06/internal/two/util.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) (*graph, error) {
|
||||
graph := newGraph()
|
||||
|
||||
count := 0
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
indx := strings.Index(line, "^")
|
||||
if indx != -1 {
|
||||
graph.startPoint = point{indx, count, N}
|
||||
}
|
||||
graph.data = append(graph.data, scanner.Text())
|
||||
|
||||
for i, r := range line {
|
||||
if r == '#' {
|
||||
graph.obstacles = append(graph.obstacles, point{i, count, obstacle})
|
||||
}
|
||||
}
|
||||
|
||||
count++
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
func replaceAtIndex(s string, r rune, i int) string {
|
||||
out := []rune(s)
|
||||
out[i] = r
|
||||
return string(out)
|
||||
}
|
||||
Reference in New Issue
Block a user