Day 5: maze => 351282

This commit is contained in:
Meutel 2017-12-08 10:16:31 +01:00
parent 9aff617dfc
commit d7ff9586ef
2 changed files with 53 additions and 0 deletions

5
05dec/input_test Normal file
View File

@ -0,0 +1,5 @@
0
3
0
1
-3

48
05dec/main.go Normal file
View File

@ -0,0 +1,48 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func main() {
moves := parseInput()
for _, m := range moves {
fmt.Print(*m, " ")
}
fmt.Println()
fmt.Println(maze(moves, 0, 0))
}
func parseInput() []*int {
var in string
moves := []*int{}
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
i, err := strconv.Atoi(sc.Text())
//fmt.Println(i)
if err != nil {
log.Fatalln("Invalid input", in)
}
moves = append(moves, &i)
}
return moves
}
func maze(moves []*int, pos int, incr int) int {
//fmt.Println("Taille", len(moves))
//fmt.Println("pos", pos)
offset := *moves[pos]
//fmt.Println("offset", offset)
newpos := pos + offset
if newpos >= len(moves) {
return incr + 1
}
offset++
moves[pos] = &offset
//fmt.Println("Newoffset at ", pos, *moves[pos])
return maze(moves, newpos, incr+1)
}