Implement Blocks function

This commit is contained in:
Dan Jones 2024-12-09 12:49:56 -06:00
commit a495a2ef1a
2 changed files with 27 additions and 2 deletions

View file

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

View file

@ -1,10 +1,10 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"os" "os"
"bytes"
"codeberg.org/danjones000/advent-of-code/2024-09/defrag" "codeberg.org/danjones000/advent-of-code/2024-09/defrag"
) )