diff --git a/06dec/input_test b/06dec/input_test new file mode 100644 index 0000000..2dbebf1 --- /dev/null +++ b/06dec/input_test @@ -0,0 +1 @@ +0 2 7 0 diff --git a/06dec/main.go b/06dec/main.go new file mode 100644 index 0000000..a8a9c54 --- /dev/null +++ b/06dec/main.go @@ -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 +}