105 lines
1.5 KiB
Go
105 lines
1.5 KiB
Go
|
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
|
||
|
}
|