start day 10
This commit is contained in:
45
10/input
Normal file
45
10/input
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
123454078104569871014321021321012349878967874
|
||||||
|
002965169245678562105489110456701256767456983
|
||||||
|
411873254324329453456976522343812323454305432
|
||||||
|
320984589010012306567887431098943210563216761
|
||||||
|
458976673407873210698790346787654329874109850
|
||||||
|
367887654356912348787891245497645698556780943
|
||||||
|
656792343105401059678712312338562107649891234
|
||||||
|
343001643234332769589600404329431236032321104
|
||||||
|
102198756121049878465521565010120345121430213
|
||||||
|
234567987012156912374437674320345034560345412
|
||||||
|
126898776103457809983398983011276127672126703
|
||||||
|
045678945434967618812287034567689018984035894
|
||||||
|
034989876325898507600176127698548109123445665
|
||||||
|
121070165016785216010165018345630678006510778
|
||||||
|
015676234245234345323234509216721565217329889
|
||||||
|
104389892104101059654965432101892674398456788
|
||||||
|
010210743233298128760870123438984583212105998
|
||||||
|
327805654654567637201256754347673294303614854
|
||||||
|
476912348723498542112349861243100185454783763
|
||||||
|
585210239010101443009659870652231256765692152
|
||||||
|
694332108212012356788778778701645899801087001
|
||||||
|
783445098302345653299894389614556732112396503
|
||||||
|
612956789401298764108763210543678945013765412
|
||||||
|
107810987567889655895654101012109876894894321
|
||||||
|
016721076101910346781565092325012345765765010
|
||||||
|
145432345234581232690478783234540332345654321
|
||||||
|
298703876501698701541089654129651231056310145
|
||||||
|
387212965012787650132108543098774340987234236
|
||||||
|
456903454323456043236719612012589656891105687
|
||||||
|
125876561210798101045898703343478797650112793
|
||||||
|
034329870325887232784387643296556788943289832
|
||||||
|
105610711456976545693211234187645019850176541
|
||||||
|
612789804567803456789800105098732134567654320
|
||||||
|
523898714326512101234764306789678325478956010
|
||||||
|
630145625613456780345605219812569210329547854
|
||||||
|
549234034702102395653216984503431678017632903
|
||||||
|
678432129899801234764567893689120589098701012
|
||||||
|
569562101234710109803891012778021432176512343
|
||||||
|
450691032105676541012764569865434501089400154
|
||||||
|
541782145694589132123453678345123671078321965
|
||||||
|
432679238785410016787652101254010982361267873
|
||||||
|
121018019856321125696545610763010973450398654
|
||||||
|
038967025657689434545236789892129887650367743
|
||||||
|
127652134768976521430129865430034796341459812
|
||||||
|
234543089867987010321010178921015601232378103
|
||||||
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