day17, example2

This commit is contained in:
efim 2023-12-17 13:58:34 +00:00
parent a7e06e7a6e
commit 955bdc78c1
2 changed files with 59 additions and 16 deletions

View File

@ -11,17 +11,16 @@ import (
func Run() int {
fmt.Println("hello from day 17")
filename := "day17/input"
filename := "day17/example"
field := NewField(filename)
log.Printf("%+v\n", field)
field.RunDijkstra()
end := Coord{field.Height - 1, field.Width - 1}
lenToEnd := field.Paths[end].totalLength
lenToEnd := field.Paths[field.Finish].totalLength
fmt.Println("check visually:")
// fmt.Println(field.Paths[end].stringPathSoFar)
fmt.Println(field.Paths[end].stringPathSoFar)
fmt.Println(field.Paths[field.Finish].stringPathSoFar)
return lenToEnd
}
@ -127,6 +126,24 @@ type PathSegmentEnd struct {
done bool
}
func (p *PathSegmentEnd) NextDirections2() (next []Direction) {
// last steps of 2 is max allowed 3 tiles in row
lastSteps := p.lastSteps[p.lastDirection]
if lastSteps < 4 {
return []Direction{p.lastDirection}
}
next = append(next, p.lastDirection.GetPerpendicular()...)
if lastSteps < 10 {
next = append(next, p.lastDirection)
}
log.Printf("getting directions from %+v they are %+v", p, next)
return
}
func (p *PathSegmentEnd) NextDirections() (next []Direction) {
next = append(next, p.lastDirection.GetPerpendicular()...)
@ -145,6 +162,7 @@ type Field struct {
Costs [][]int
Height, Width int
Start Coord
Finish Coord
}
func NewField(filename string) Field {
@ -158,13 +176,16 @@ func NewField(filename string) Field {
}
initialPaths := make(map[Coord]*PathSegmentEnd)
initialPaths[Coord{0, 0}] = &startSegment
height := len(enterCosts)
width := len(enterCosts[0])
return Field{
Paths: initialPaths,
Costs: enterCosts,
Height: len(enterCosts),
Width: len(enterCosts[0]),
Height: height,
Width: width,
Start: Coord{0, 0},
Finish: Coord{height - 1, width - 1},
}
}
@ -206,9 +227,19 @@ func (f *Field) RunDijkstra() {
distancesMap := make(map[string]int, 0)
startingPath := f.Paths[f.Start]
checking = append(checking, *startingPath)
anotherStartingPath := PathSegmentEnd{
endsAt: Coord{0, 0},
totalLength: 0,
lastSteps: make(map[Direction]int),
done: true,
lastDirection: Rightward, // fake, need to init direct neighbors also
stringPathSoFar: ".",
}
checking = append(checking, *startingPath, anotherStartingPath)
distancesMap[startingPath.StringKey()] = 0
distancesMap[anotherStartingPath.StringKey()] = 0
for len(checking) > 0 {
var currentPath PathSegmentEnd
@ -220,23 +251,30 @@ func (f *Field) RunDijkstra() {
}
}
currentCoord := currentPath.endsAt
directions := currentPath.NextDirections()
directions := currentPath.NextDirections2()
// fmt.Printf("> one more iteration for %+v ; directions will check %+v\n", currentPath, directions)
for _, direction := range directions {
neighborCoord := currentCoord.applyDirection(direction)
if !f.isValid(neighborCoord) {
continue // prevent going off the grid
}
// log.Printf("from %+v will examine in direction %s to %+v", currentCoord, direction, neighborCoord)
// neighborPathSoFar, found := f.Paths[neighborCoord]
// if !found {
// neighborPathSoFar = &PathSegmentEnd{
// totalLength: math.MaxInt,
// }
// f.Paths[neighborCoord] = neighborPathSoFar
// }
// fmt.Printf("from %+v will examine in direction %s to %+v %+v\n", currentCoord, direction, neighborCoord, currentPath)
neighborPathSoFar, found := f.Paths[neighborCoord]
if !found {
neighborPathSoFar = &PathSegmentEnd{
totalLength: math.MaxInt,
}
f.Paths[neighborCoord] = neighborPathSoFar
}
pathIfWeGoFromCurrent := f.continuePathInDirection(currentPath, direction)
if pathIfWeGoFromCurrent.endsAt == f.Finish {
if pathIfWeGoFromCurrent.lastSteps[pathIfWeGoFromCurrent.lastDirection] < 4 {
continue
}
}
distFromThatSide, isKnown := distancesMap[pathIfWeGoFromCurrent.StringKey()]
if !isKnown {
distancesMap[pathIfWeGoFromCurrent.StringKey()] = pathIfWeGoFromCurrent.totalLength

5
day17/example2 Normal file
View File

@ -0,0 +1,5 @@
111111111111
999999999991
999999999991
999999999991
999999999991