2017 Day 05 Complete!
This commit is contained in:
64
2017/day05/day05.go
Normal file
64
2017/day05/day05.go
Normal file
@@ -0,0 +1,64 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user