adventofcode/10dec/main.go

77 lines
1.1 KiB
Go
Raw Normal View History

2017-12-22 22:14:25 +00:00
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
const (
//SZ = 5
SZ = 256
)
var (
list [SZ]int
lengths []int
pos int
skip int
)
func main() {
lengths = parseLengths()
//fmt.Println(lengths)
for i, _ := range list {
list[i] = i
}
for _, l := range lengths {
//fmt.Println(list, pos, skip, l)
reverse(l)
pos = (pos + l + skip) % SZ
skip++
}
//fmt.Println(list)
fmt.Println("Result:", list[0]*list[1])
}
func reverse(l int) {
if l < 2 {
return
}
rev := make([]int, l, l)
to := (pos + l) % SZ
if to > pos {
for i, n := range list[pos:to] {
rev[l-i-1] = n
}
} else {
var i, j, n int
for i, n = range list[pos:SZ] {
rev[l-i-1] = n
}
//fmt.Println(" ", rev)
for j, n = range list[0:to] {
rev[l-j-i-2] = n
}
}
//fmt.Println(" ", rev)
for i, n := range rev {
list[(pos+i)%SZ] = n
}
}
func parseLengths() []int {
l := make([]int, 0, 16)
sc := bufio.NewScanner(os.Stdin)
sc.Scan()
for _, num := range strings.Split(sc.Text(), ",") {
n, _ := strconv.Atoi(num)
l = append(l, n)
}
return l
}