chore: fix uncertanty on day17
All checks were successful
Go Test / build (pull_request) Successful in 36s

This commit is contained in:
xuu 2023-12-29 21:10:59 -07:00
parent 1fac5f7b4d
commit a2563b9d31
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F
2 changed files with 26 additions and 20 deletions

View File

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

View File

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