day 10: knot

This commit is contained in:
Meutel 2017-12-22 23:14:25 +01:00
parent e22fddcde7
commit ba23247555
2 changed files with 77 additions and 0 deletions

1
10dec/input_test Normal file
View File

@ -0,0 +1 @@
3,4,1,5

76
10dec/main.go Normal file
View File

@ -0,0 +1,76 @@
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
}