adventofcode/2017/day17/day17.go

58 lines
1.1 KiB
Go
Raw Normal View History

2017-12-17 16:00:37 +00:00
package main
import (
"fmt"
"log"
"os"
"strconv"
)
func main() {
inp := 301
tgt := 50000000
if len(os.Args) > 2 {
inp = Atoi(os.Args[1])
tgt = Atoi(os.Args[2])
} else if len(os.Args) > 1 {
fmt.Println("Usage: day17 [skip] [target]")
fmt.Println("If you provide one argument, you must provide both")
}
spinLock := []int{0}
var addPos int
for i := 1; i <= tgt; i++ {
addPos = (addPos + inp) % len(spinLock)
// Unless we're adding it after the 0, we don't care, just append it.
if addPos == 0 {
addPos++
spinLock = Insert(spinLock, i, addPos)
} else {
addPos++
spinLock = append(spinLock, i)
}
}
fmt.Println(spinLock[0:0+5], "...")
}
func Insert(slice []int, val, at int) []int {
var ret []int
pt1 := slice[:at]
pt2 := slice[at:]
for i := 0; i < len(pt1); i++ {
ret = append(ret, pt1[i])
}
ret = append(ret, val)
for i := 0; i < len(pt2); i++ {
ret = append(ret, pt2[i])
}
return ret
}
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
}