adventofcode/05dec/main.go

49 lines
838 B
Go
Raw Normal View History

2017-12-08 09:16:31 +00:00
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)
}