adventofcode/2017/day05/day05.go

65 lines
991 B
Go
Raw Normal View History

2017-12-05 12:38:44 +00:00
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func main() {
inp := StdinToInts()
inp2 := make([]int, len(inp))
copy(inp2, inp)
part1(inp)
part2(inp2)
}
func part1(inp []int) {
fmt.Println("= Part 1 =")
idx, steps := 0, 0
for idx >= 0 && idx < len(inp) {
jmp := inp[idx]
inp[idx] = inp[idx] + 1
idx = idx + jmp
steps++
}
fmt.Println("Total Steps:", steps)
}
func part2(inp []int) {
fmt.Println("= Part 2 =")
idx, steps := 0, 0
for idx >= 0 && idx < len(inp) {
jmp := inp[idx]
if jmp >= 3 {
inp[idx] = inp[idx] - 1
} else {
inp[idx] = inp[idx] + 1
}
idx = idx + jmp
steps++
}
fmt.Println("Total Steps:", steps)
}
func StdinToInts() []int {
var input []int
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, Atoi(scanner.Text()))
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}