🐛 Account for file ids > 9

This commit is contained in:
Dan Jones 2024-12-09 14:34:01 -06:00
commit 495d50ba10
7 changed files with 40 additions and 37 deletions

View file

@ -1,30 +1,31 @@
package defrag
import (
"bytes"
"strconv"
"strings"
)
func Blocks(diskMap []byte) ([]byte, error) {
func Blocks(diskMap []byte) ([]int, error) {
current := 0
onFile := false
buff := bytes.Buffer{}
out := []int{}
for _, by := range diskMap {
count, err := strconv.Atoi(string(by))
if err != nil {
return nil, err
}
onFile = !onFile
if count == 0 {
continue
}
append := "."
val := -1
if onFile {
append = strconv.Itoa(current)
val = current
current++
}
buff.WriteString(strings.Repeat(append, count))
for idx := 0; idx < count; idx++ {
out = append(out, val)
}
}
return buff.Bytes(), nil
return out, nil
}