up till 08/01/2025

This commit is contained in:
orejav
2025-01-09 10:11:54 +02:00
parent f39c523d4e
commit 3c12255382
2 changed files with 8 additions and 19 deletions

View File

@@ -20,33 +20,28 @@ func findTrailheads(tmap [][]int) [][]int {
return trailheadsPositions return trailheadsPositions
} }
func nextSteps(tmap [][]int, currentPosition []int) ([][]int, bool) { func nextSteps(tmap [][]int, currentPosition []int) [][]int {
paths := [][]int{} paths := [][]int{}
found := false
if currentPosition[0] - 1 >= 0 && tmap[currentPosition[0]-1][currentPosition[1]] == tmap[currentPosition[0]][currentPosition[1]] + 1 { if currentPosition[0] - 1 >= 0 && tmap[currentPosition[0]-1][currentPosition[1]] == tmap[currentPosition[0]][currentPosition[1]] + 1 {
paths = append(paths, []int{currentPosition[0]-1,currentPosition[1]}) paths = append(paths, []int{currentPosition[0]-1,currentPosition[1]})
found = true
} }
if currentPosition[0] + 1 < len(tmap) && tmap[currentPosition[0]+1][currentPosition[1]] == tmap[currentPosition[0]][currentPosition[1]] + 1 { if currentPosition[0] + 1 < len(tmap) && tmap[currentPosition[0]+1][currentPosition[1]] == tmap[currentPosition[0]][currentPosition[1]] + 1 {
paths = append(paths, []int{currentPosition[0]+1,currentPosition[1]}) paths = append(paths, []int{currentPosition[0]+1,currentPosition[1]})
found = true
} }
if currentPosition[1] + 1 < len(tmap[currentPosition[0]]) && tmap[currentPosition[0]][currentPosition[1]+1] == tmap[currentPosition[0]][currentPosition[1]] + 1 { if currentPosition[1] + 1 < len(tmap[currentPosition[0]]) && tmap[currentPosition[0]][currentPosition[1]+1] == tmap[currentPosition[0]][currentPosition[1]] + 1 {
paths = append(paths, []int{currentPosition[0],currentPosition[1]+1}) paths = append(paths, []int{currentPosition[0],currentPosition[1]+1})
found = true
} }
if currentPosition[1] - 1 >= 0 && tmap[currentPosition[0]][currentPosition[1]-1] == tmap[currentPosition[0]][currentPosition[1]] + 1 { if currentPosition[1] - 1 >= 0 && tmap[currentPosition[0]][currentPosition[1]-1] == tmap[currentPosition[0]][currentPosition[1]] + 1 {
paths = append(paths, []int{currentPosition[0],currentPosition[1]-1}) paths = append(paths, []int{currentPosition[0],currentPosition[1]-1})
found = true
} }
return paths, found return paths
} }
func calcPath(tmap [][]int, start []int) [][]int { func calcPath(tmap [][]int, start []int) [][]int {
path := [][]int{start} path := [][]int{start}
for i:=1;i<=9;i++ { for i:=1;i<=9;i++ {
nextStep, found := nextSteps(tmap,path[len(path)-1]) nextStep := nextSteps(tmap,)
if ! found { if len(nextStep) > 0 {
break break
} else { } else {
path = append(path, nextStep...) path = append(path, nextStep...)

View File

@@ -3,15 +3,9 @@ package main
import "fmt" import "fmt"
func main() { func main() {
// Initialize the variable before the loop slice := []int{}
value := 0 for i := range slice {
fmt.Println(i)
for i := 0; i < 5; i++ { }
// Change its value within the loop
value += i
fmt.Println("Current Value:", value) // Use the variable here
}
fmt.Println("Final Value:", value) // Still accessible after the loop
} }