Compare commits

..

No commits in common. "c89f5611ed7d1332e9bb5b8ecd01d501efb744e2" and "503082d822297063eca09564c4e5794db5cbe4a8" have entirely different histories.

6 changed files with 43 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package one package one
import ( import (
"slices"
"strings" "strings"
) )
@ -29,3 +30,11 @@ func (g *graph) isOutOfBounds(p point) bool {
func (g *graph) valueAt(p point) rune { func (g *graph) valueAt(p point) rune {
return rune(g.data[p.y][p.x]) return rune(g.data[p.y][p.x])
} }
func (g *graph) debug(path []point) string {
temp := slices.Clone(g.data)
for _, pos := range path {
temp[pos.y] = replaceAtIndex(temp[pos.y], '+', pos.x)
}
return strings.Join(temp, "\n")
}

View File

@ -37,3 +37,9 @@ func parseLines(r io.Reader) (*graph, error) {
return graph, nil return graph, nil
} }
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}

View File

@ -1,12 +1,14 @@
package two package two
import ( import (
"slices"
"strings" "strings"
) )
type point struct { type point struct {
x int x int
y int y int
direction int
} }
type graph struct { type graph struct {
@ -29,3 +31,11 @@ func (g *graph) isOutOfBounds(p point) bool {
func (g *graph) valueAt(p point) rune { func (g *graph) valueAt(p point) rune {
return rune(g.data[p.y][p.x]) return rune(g.data[p.y][p.x])
} }
func (g *graph) debug(path []point) string {
temp := slices.Clone(g.data)
for _, pos := range path {
temp[pos.y] = replaceAtIndex(temp[pos.y], '+', pos.x)
}
return strings.Join(temp, "\n")
}

View File

@ -2,9 +2,9 @@ package two
func neighbours(p point) [4]point { func neighbours(p point) [4]point {
return [4]point{ return [4]point{
{p.x, p.y - 1}, {p.x, p.y - 1, N},
{p.x + 1, p.y}, {p.x + 1, p.y, E},
{p.x, p.y + 1}, {p.x, p.y + 1, S},
{p.x - 1, p.y}, {p.x - 1, p.y, W},
} }
} }

View File

@ -34,11 +34,15 @@ func Solve(buf []byte) (int, error) {
continue continue
} }
for _, n := range neighbours(current) { for dir, n := range neighbours(current) {
if graph.isOutOfBounds(n) { if graph.isOutOfBounds(n) {
continue continue
} }
if n.direction == None {
n.direction = dir
}
if graph.valueAt(n)-graph.valueAt(current) == 1 { if graph.valueAt(n)-graph.valueAt(current) == 1 {
log.Tracef("pushing %v with value %s back onto the queue\n", n, string(graph.valueAt(n))) log.Tracef("pushing %v with value %s back onto the queue\n", n, string(graph.valueAt(n)))
queue.Enqueue(n) queue.Enqueue(n)

View File

@ -22,7 +22,7 @@ func parseLines(r io.Reader) (*graph, error) {
for _, m := range reStartPos.FindAllStringIndex(line, -1) { for _, m := range reStartPos.FindAllStringIndex(line, -1) {
graph.startPositions = append( graph.startPositions = append(
graph.startPositions, graph.startPositions,
point{x: m[0], y: linecount}, point{x: m[0], y: linecount, direction: None},
) )
} }
@ -37,3 +37,9 @@ func parseLines(r io.Reader) (*graph, error) {
return graph, nil return graph, nil
} }
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}