add day-06 + benchmarks

This commit is contained in:
2024-12-06 20:01:17 +00:00
parent d1308ffdfe
commit 567ca72402
21 changed files with 532 additions and 0 deletions

View 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

View File

@@ -0,0 +1,9 @@
package two
const (
N = iota
E
S
W
obstacle
)

View 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()
}

View 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{}
}
}

View 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
}

View 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)
}

View 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)
}