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
}

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

View File

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