Compare commits

..

No commits in common. "378e403c1cf804fedcb9c595ee52ecc062cd078e" and "3c2ea4ed9ebfebbedf0b4aefcda7529dfd62caa7" have entirely different histories.

2 changed files with 20 additions and 26 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
aoc "go.sour.is/advent-of-code"
"golang.org/x/exp/maps"
)
// var log = aoc.Log
@ -50,12 +51,13 @@ func search(m aoc.Map[rune], minSteps, maxSteps int) int {
L = aoc.Point{0, -1}
)
var Direction = []aoc.Point{U, R, D, L}
var Directions = make(map[aoc.Point]direction, len(Direction))
for k, v := range Direction {
Directions[v] = direction(k)
var Directions = map[aoc.Point]direction{
U: 0, // U
R: 1, // R
D: 2, // D
L: 3, // L
}
var DirectionIDX = maps.Keys(Directions)
rows, cols := m.Size()
target := aoc.Point{rows - 1, cols - 1}
@ -70,8 +72,7 @@ func search(m aoc.Map[rune], minSteps, maxSteps int) int {
return position{p.loc.Add(p.direction), p.direction, p.steps + 1}
}
rotateAndStep := func(p position, towards rotate) position {
d := Direction[(int8(Directions[p.direction])+int8(towards)+4)%4]
// fmt.Println(towards, Directions[p.direction], "->", Directions[d])
d := DirectionIDX[(int8(Directions[p.direction])+int8(towards)+4)%4]
return position{p.loc.Add(d), d, 1}
}
@ -89,7 +90,7 @@ func search(m aoc.Map[rune], minSteps, maxSteps int) int {
if a.position.direction != b.position.direction {
return b.position.direction.Less(a.position.direction)
}
return a.steps < b.steps
return b.steps < a.steps
}
pq := aoc.PriorityQueue(less)
@ -104,34 +105,27 @@ func search(m aoc.Map[rune], minSteps, maxSteps int) int {
return current.cost
}
seen := position{loc: current.loc, direction: current.direction, steps: current.steps}
seen := position{loc: current.loc, steps: current.steps}
if visited.Has(seen) {
// fmt.Println("visited", seen)
continue
}
visited.Add(seen)
// fmt.Print("\033[2J\033[H")
// fmt.Println("step ", current.steps, " dir ", Directions[current.direction], " steps ", " score ", current.cost, current.loc)
if left := rotateAndStep(current.position, CCW); current.steps >= minSteps && m.Valid(left.loc) {
_, cost, _ := m.Get(left.loc)
// fmt.Println("turn left", current, left)
pq.Enqueue(memo{cost: current.cost + int(cost-'0'), position: left})
}
if right := rotateAndStep(current.position, CW); current.steps >= minSteps && m.Valid(right.loc) {
_, cost, _ := m.Get(right.loc)
// fmt.Println("turn right", current, right)
pq.Enqueue(memo{cost: current.cost + int(cost-'0'), position: right})
}
if forward := step(current.position); current.steps < maxSteps && m.Valid(forward.loc) {
_, cost, _ := m.Get(forward.loc)
// fmt.Println("go forward", current, forward)
pq.Enqueue(memo{cost: current.cost + int(cost-'0'), position: forward})
}
}
return -1
}

View File

@ -28,14 +28,14 @@ func TestExample(t *testing.T) {
is.Equal(result.valuePT2, 94)
}
// func TestSolution(t *testing.T) {
// is := is.New(t)
// scan := bufio.NewScanner(bytes.NewReader(input))
func TestSolution(t *testing.T) {
is := is.New(t)
scan := bufio.NewScanner(bytes.NewReader(input))
// result, err := run(scan)
// is.NoErr(err)
result, err := run(scan)
is.NoErr(err)
// t.Log(result)
// is.Equal(result.valuePT1, 843)
// is.Equal(result.valuePT2, 1017)
// }
t.Log(result)
is.Equal(result.valuePT1, 843)
is.Equal(result.valuePT2, 1017)
}