start day 10

This commit is contained in:
Javier-Orestis MANTZIOS
2025-01-08 22:51:27 +02:00
parent 89ca6f60df
commit f39c523d4e
3 changed files with 135 additions and 0 deletions

82
10/main.go Normal file
View 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 {
}
}
}