start day 10
This commit is contained in:
82
10/main.go
Normal file
82
10/main.go
Normal file
@@ -0,0 +1,82 @@
|
||||
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, bool) {
|
||||
paths := [][]int{}
|
||||
found := false
|
||||
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]})
|
||||
found = true
|
||||
}
|
||||
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]})
|
||||
found = true
|
||||
}
|
||||
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})
|
||||
found = true
|
||||
}
|
||||
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})
|
||||
found = true
|
||||
}
|
||||
return paths, found
|
||||
}
|
||||
|
||||
func calcPath(tmap [][]int, start []int) [][]int {
|
||||
path := [][]int{start}
|
||||
for i:=1;i<=9;i++ {
|
||||
nextStep, found := nextSteps(tmap,path[len(path)-1])
|
||||
if ! found {
|
||||
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 {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user