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/one
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.007368 ns/op
ok github.com/onyx-and-iris/aoc2024/day-06/internal/one 0.052s

View File

@@ -0,0 +1,8 @@
package one
const (
N = iota
E
S
W
)

View File

@@ -0,0 +1,30 @@
package one
import (
"strings"
"github.com/onyx-and-iris/aoc2024/day-06/internal/util"
)
type graph struct {
data []string
startPoint point
}
func newGraph() *graph {
return &graph{}
}
func (g *graph) String() string {
return strings.Join(g.data, "\n")
}
func (g *graph) debug(visited map[coords]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,51 @@
package one
import "fmt"
type coords struct {
X int
Y int
}
type point struct {
coords
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"}[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{coords{current.X, current.Y - 1}, current.direction}
case E:
return point{coords{current.X + 1, current.Y}, current.direction}
case S:
return point{coords{current.X, current.Y + 1}, current.direction}
case W:
return point{coords{current.X - 1, current.Y}, current.direction}
default:
return point{}
}
}

View File

@@ -0,0 +1,51 @@
package one
import (
"bytes"
log "github.com/sirupsen/logrus"
)
var Visited = make(map[coords]struct{})
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
graph, err := parseLines(r)
if err != nil {
return 0, err
}
log.Debug(graph.startPoint.String())
var count int
count = nextStep(graph.startPoint, graph, Visited, count)
log.Debugf("path walked: \n%s\n", graph.debug(Visited))
return count, nil
}
func nextStep(point point, graph *graph, visited map[coords]struct{}, count int) int {
_, ok := visited[point.coords]
if !ok {
visited[point.coords] = struct{}{}
count++
}
log.Debugf("count: %d\n", count)
if point.X == 0 && point.direction == W ||
point.Y == 0 && point.direction == N ||
point.Y == len(graph.data)-1 && point.direction == S ||
point.X == len(graph.data[point.Y])-1 && point.direction == E {
return count
}
next := nextPoint(point)
log.Debug(next.String())
log.Debug(string(graph.data[next.Y][next.X]))
if graph.data[next.Y][next.X] == '#' {
next.recalibrate()
log.Debugf("switched direction to %d", next.direction)
}
return nextStep(next, graph, visited, count)
}

View File

@@ -0,0 +1,15 @@
package one
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,29 @@
package one
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{coords{indx, count}, N}
}
graph.data = append(graph.data, scanner.Text())
count++
}
if err := scanner.Err(); err != nil {
return nil, err
}
return graph, nil
}