Day 6: reallocate

11137
This commit is contained in:
Meutel 2017-12-08 11:51:40 +01:00
parent d7ff9586ef
commit e112db3506
2 changed files with 105 additions and 0 deletions

1
06dec/input_test Normal file
View File

@ -0,0 +1 @@
0 2 7 0

104
06dec/main.go Normal file
View File

@ -0,0 +1,104 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
var (
mem []*int
ref [][]int
)
func main() {
parseInput()
fmt.Println("\n", reallocatRecursive(0))
}
func printMem() {
for _, m := range mem {
fmt.Print(*m, " ")
}
fmt.Println()
}
func parseInput() {
mem = []*int{}
ref = append([][]int{}, []int{})
sc := bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanWords)
for sc.Scan() {
i, err := strconv.Atoi(sc.Text())
// fmt.Println(i)
if err != nil {
log.Fatalln("Invalid input", sc.Text())
}
mem = append(mem, &i)
ref[0] = append(ref[0], i)
}
}
func isRef() bool {
//fmt.Println("Ref size", len(ref))
for _, r := range ref {
//fmt.Println(r)
if len(mem) == len(r) {
match := true
for i, m := range mem {
if r[i] != *m {
match = false
}
}
if match {
return true
}
}
}
return false
}
func reallocatRecursive(incr int) int {
//printMem()
reallocate()
if isRef() {
return incr + 1
}
appendRef()
return reallocatRecursive(incr + 1)
}
func appendRef() {
r := []int{}
for _, m := range mem {
r = append(r, *m)
}
ref = append(ref, r)
}
func reallocate() {
//fmt.Println("mem size", len(mem))
block := findBlock()
//fmt.Println("reallocate", block)
blocks := *mem[block]
//fmt.Println(blocks)
*mem[block] -= blocks // RAZ
for i := 0; i < blocks; i++ {
block = (block + 1) % len(mem)
*mem[block]++
}
}
func findBlock() int {
max := 0
maxPos := 0
for i, m := range mem {
if *m > max {
max = *m
maxPos = i
}
}
return maxPos
}