From d7ff9586efcfeba80aae618d168bdc20016cc8f7 Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 8 Dec 2017 10:16:31 +0100 Subject: [PATCH] Day 5: maze => 351282 --- 05dec/input_test | 5 +++++ 05dec/main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 05dec/input_test create mode 100644 05dec/main.go diff --git a/05dec/input_test b/05dec/input_test new file mode 100644 index 0000000..7a76cf7 --- /dev/null +++ b/05dec/input_test @@ -0,0 +1,5 @@ +0 +3 +0 +1 +-3 diff --git a/05dec/main.go b/05dec/main.go new file mode 100644 index 0000000..89d28f0 --- /dev/null +++ b/05dec/main.go @@ -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) +}