day17, example2
This commit is contained in:
parent
a7e06e7a6e
commit
955bdc78c1
|
@ -11,17 +11,16 @@ import (
|
||||||
|
|
||||||
func Run() int {
|
func Run() int {
|
||||||
fmt.Println("hello from day 17")
|
fmt.Println("hello from day 17")
|
||||||
filename := "day17/input"
|
filename := "day17/example"
|
||||||
field := NewField(filename)
|
field := NewField(filename)
|
||||||
log.Printf("%+v\n", field)
|
log.Printf("%+v\n", field)
|
||||||
|
|
||||||
field.RunDijkstra()
|
field.RunDijkstra()
|
||||||
|
|
||||||
end := Coord{field.Height - 1, field.Width - 1}
|
lenToEnd := field.Paths[field.Finish].totalLength
|
||||||
lenToEnd := field.Paths[end].totalLength
|
|
||||||
fmt.Println("check visually:")
|
fmt.Println("check visually:")
|
||||||
// fmt.Println(field.Paths[end].stringPathSoFar)
|
// fmt.Println(field.Paths[end].stringPathSoFar)
|
||||||
fmt.Println(field.Paths[end].stringPathSoFar)
|
fmt.Println(field.Paths[field.Finish].stringPathSoFar)
|
||||||
return lenToEnd
|
return lenToEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,6 +126,24 @@ type PathSegmentEnd struct {
|
||||||
done bool
|
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) {
|
func (p *PathSegmentEnd) NextDirections() (next []Direction) {
|
||||||
next = append(next, p.lastDirection.GetPerpendicular()...)
|
next = append(next, p.lastDirection.GetPerpendicular()...)
|
||||||
|
|
||||||
|
@ -145,6 +162,7 @@ type Field struct {
|
||||||
Costs [][]int
|
Costs [][]int
|
||||||
Height, Width int
|
Height, Width int
|
||||||
Start Coord
|
Start Coord
|
||||||
|
Finish Coord
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewField(filename string) Field {
|
func NewField(filename string) Field {
|
||||||
|
@ -158,13 +176,16 @@ func NewField(filename string) Field {
|
||||||
}
|
}
|
||||||
initialPaths := make(map[Coord]*PathSegmentEnd)
|
initialPaths := make(map[Coord]*PathSegmentEnd)
|
||||||
initialPaths[Coord{0, 0}] = &startSegment
|
initialPaths[Coord{0, 0}] = &startSegment
|
||||||
|
height := len(enterCosts)
|
||||||
|
width := len(enterCosts[0])
|
||||||
|
|
||||||
return Field{
|
return Field{
|
||||||
Paths: initialPaths,
|
Paths: initialPaths,
|
||||||
Costs: enterCosts,
|
Costs: enterCosts,
|
||||||
Height: len(enterCosts),
|
Height: height,
|
||||||
Width: len(enterCosts[0]),
|
Width: width,
|
||||||
Start: Coord{0, 0},
|
Start: Coord{0, 0},
|
||||||
|
Finish: Coord{height - 1, width - 1},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,9 +227,19 @@ func (f *Field) RunDijkstra() {
|
||||||
distancesMap := make(map[string]int, 0)
|
distancesMap := make(map[string]int, 0)
|
||||||
|
|
||||||
startingPath := f.Paths[f.Start]
|
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[startingPath.StringKey()] = 0
|
||||||
|
distancesMap[anotherStartingPath.StringKey()] = 0
|
||||||
|
|
||||||
for len(checking) > 0 {
|
for len(checking) > 0 {
|
||||||
var currentPath PathSegmentEnd
|
var currentPath PathSegmentEnd
|
||||||
|
@ -220,23 +251,30 @@ func (f *Field) RunDijkstra() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
currentCoord := currentPath.endsAt
|
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 {
|
for _, direction := range directions {
|
||||||
neighborCoord := currentCoord.applyDirection(direction)
|
neighborCoord := currentCoord.applyDirection(direction)
|
||||||
if !f.isValid(neighborCoord) {
|
if !f.isValid(neighborCoord) {
|
||||||
continue // prevent going off the grid
|
continue // prevent going off the grid
|
||||||
}
|
}
|
||||||
// log.Printf("from %+v will examine in direction %s to %+v", currentCoord, direction, neighborCoord)
|
// fmt.Printf("from %+v will examine in direction %s to %+v %+v\n", currentCoord, direction, neighborCoord, currentPath)
|
||||||
// neighborPathSoFar, found := f.Paths[neighborCoord]
|
neighborPathSoFar, found := f.Paths[neighborCoord]
|
||||||
// if !found {
|
if !found {
|
||||||
// neighborPathSoFar = &PathSegmentEnd{
|
neighborPathSoFar = &PathSegmentEnd{
|
||||||
// totalLength: math.MaxInt,
|
totalLength: math.MaxInt,
|
||||||
// }
|
}
|
||||||
// f.Paths[neighborCoord] = neighborPathSoFar
|
f.Paths[neighborCoord] = neighborPathSoFar
|
||||||
// }
|
}
|
||||||
|
|
||||||
pathIfWeGoFromCurrent := f.continuePathInDirection(currentPath, direction)
|
pathIfWeGoFromCurrent := f.continuePathInDirection(currentPath, direction)
|
||||||
|
if pathIfWeGoFromCurrent.endsAt == f.Finish {
|
||||||
|
if pathIfWeGoFromCurrent.lastSteps[pathIfWeGoFromCurrent.lastDirection] < 4 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
distFromThatSide, isKnown := distancesMap[pathIfWeGoFromCurrent.StringKey()]
|
distFromThatSide, isKnown := distancesMap[pathIfWeGoFromCurrent.StringKey()]
|
||||||
if !isKnown {
|
if !isKnown {
|
||||||
distancesMap[pathIfWeGoFromCurrent.StringKey()] = pathIfWeGoFromCurrent.totalLength
|
distancesMap[pathIfWeGoFromCurrent.StringKey()] = pathIfWeGoFromCurrent.totalLength
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
111111111111
|
||||||
|
999999999991
|
||||||
|
999999999991
|
||||||
|
999999999991
|
||||||
|
999999999991
|
Loading…
Reference in New Issue