78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func findTrailheads(tmap [][]int) [][]int {
|
|
trailheadsPositions := [][]int{}
|
|
for i:=range tmap {
|
|
for j:=range tmap[i] {
|
|
if tmap[i][j] == 0 {
|
|
trailheadsPositions = append(trailheadsPositions, []int{i,j})
|
|
}
|
|
}
|
|
}
|
|
return trailheadsPositions
|
|
}
|
|
|
|
func nextSteps(tmap [][]int, currentPosition []int) [][]int {
|
|
paths := [][]int{}
|
|
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]})
|
|
}
|
|
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]})
|
|
}
|
|
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})
|
|
}
|
|
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})
|
|
}
|
|
return paths
|
|
}
|
|
|
|
func calcPath(tmap [][]int, start []int) [][]int {
|
|
path := [][]int{start}
|
|
for i:=1;i<=9;i++ {
|
|
nextStep := nextSteps(tmap,)
|
|
if len(nextStep) > 0 {
|
|
break
|
|
} else {
|
|
path = append(path, nextStep...)
|
|
}
|
|
}
|
|
return path
|
|
}
|
|
|
|
func main() {
|
|
file, _ := os.Open("test")
|
|
// file, _ := os.Open("input")
|
|
defer file.Close()
|
|
scanner := bufio.NewScanner(file)
|
|
tmap := [][]int{}
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
level := strings.Split(line,"")
|
|
levelInt := []int{}
|
|
for i:=range level {
|
|
levelInteger, _ := strconv.Atoi(level[i])
|
|
levelInt = append(levelInt, levelInteger)
|
|
}
|
|
tmap = append(tmap, levelInt)
|
|
}
|
|
trailheadsPositions := findTrailheads(tmap)
|
|
for i := range trailheadsPositions {
|
|
path := calcPath(tmap,trailheadsPositions[i])
|
|
fmt.Println(path)
|
|
if len(path) == 10 {
|
|
|
|
}
|
|
}
|
|
}
|