✨ Implement Blocks function
This commit is contained in:
parent
ac16a3f167
commit
a495a2ef1a
2 changed files with 27 additions and 2 deletions
27
blocks.go
27
blocks.go
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue