From 955bdc78c1d5f37e66d1cc3d808ed16942f37b57 Mon Sep 17 00:00:00 2001 From: efim Date: Sun, 17 Dec 2023 13:58:34 +0000 Subject: [PATCH] day17, example2 --- day17/clumsyCrucible.go | 70 +++++++++++++++++++++++++++++++---------- day17/example2 | 5 +++ 2 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 day17/example2 diff --git a/day17/clumsyCrucible.go b/day17/clumsyCrucible.go index 1f115db..049f655 100644 --- a/day17/clumsyCrucible.go +++ b/day17/clumsyCrucible.go @@ -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 diff --git a/day17/example2 b/day17/example2 new file mode 100644 index 0000000..e456899 --- /dev/null +++ b/day17/example2 @@ -0,0 +1,5 @@ +111111111111 +999999999991 +999999999991 +999999999991 +999999999991