🚧 Add Defrag function
This commit is contained in:
parent
2f165369cc
commit
932e8e2f11
7 changed files with 136 additions and 1 deletions
5
blocks.go
Normal file
5
blocks.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package defrag
|
||||
|
||||
func Blocks(diskMap []byte) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
31
blocks_test.go
Normal file
31
blocks_test.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package defrag
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type tt struct {
|
||||
name string
|
||||
diskMap []byte
|
||||
blocks []byte
|
||||
defragged []byte
|
||||
}
|
||||
|
||||
func sampleTests() []tt {
|
||||
return []tt{
|
||||
{"short", []byte("12345"), []byte("0..111....22222"), []byte("022111222......")},
|
||||
{"longer", []byte("2333133121414131402"), []byte("00...111...2...333.44.5555.6666.777.888899"), []byte("0099811188827773336446555566..............")},
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlocks(t *testing.T) {
|
||||
for _, testcase := range sampleTests() {
|
||||
t.Run(testcase.name, func(sub *testing.T) {
|
||||
blocks, err := Blocks(testcase.diskMap)
|
||||
require.NoError(sub, err)
|
||||
require.Equal(sub, testcase.blocks, blocks)
|
||||
})
|
||||
}
|
||||
}
|
||||
36
cmd/defrag/main.go
Normal file
36
cmd/defrag/main.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"bytes"
|
||||
|
||||
"codeberg.org/danjones000/advent-of-code/2024-09/defrag"
|
||||
)
|
||||
|
||||
func handleErr(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
name := os.Args[0]
|
||||
|
||||
input, err := io.ReadAll(os.Stdin)
|
||||
handleErr(err)
|
||||
|
||||
fmt.Fprintln(os.Stderr, "Welcome to ", name)
|
||||
input = bytes.TrimSpace(input)
|
||||
|
||||
blocks, err := defrag.Blocks(input)
|
||||
handleErr(err)
|
||||
|
||||
fmt.Println("@TODO")
|
||||
fmt.Println("Got this")
|
||||
fmt.Printf("%s\n", blocks)
|
||||
}
|
||||
26
defrag.go
Normal file
26
defrag.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package defrag
|
||||
|
||||
// Defrag performs an in-place sort of blocks.
|
||||
// If an error occurs during processing, it will be returned. The slice may have been partially sorted.
|
||||
func Defrag(blocks []byte) error {
|
||||
lastPulled := len(blocks)
|
||||
lastPushed := -1
|
||||
for block := 0; block < len(blocks); block++ {
|
||||
if blocks[block] != '.' {
|
||||
continue
|
||||
}
|
||||
if lastPulled <= block {
|
||||
break
|
||||
}
|
||||
for swap := lastPulled - 1; swap > lastPushed; swap-- {
|
||||
if blocks[swap] == '.' {
|
||||
continue
|
||||
}
|
||||
blocks[block], blocks[swap] = blocks[swap], blocks[block]
|
||||
lastPulled = swap
|
||||
lastPushed = block
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
19
defrag_test.go
Normal file
19
defrag_test.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package defrag
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDefrag(t *testing.T) {
|
||||
for _, testcase := range sampleTests() {
|
||||
t.Run(testcase.name, func(sub *testing.T) {
|
||||
blocks := make([]byte, len(testcase.blocks))
|
||||
copy(blocks, testcase.blocks)
|
||||
err := Defrag(blocks)
|
||||
require.NoError(sub, err)
|
||||
require.Equal(sub, testcase.defragged, blocks)
|
||||
})
|
||||
}
|
||||
}
|
||||
10
go.mod
10
go.mod
|
|
@ -1,3 +1,11 @@
|
|||
module codeberg.org/danjones000/advent-of-code-2024-09
|
||||
module codeberg.org/danjones000/advent-of-code/2024-09/defrag
|
||||
|
||||
go 1.23.1
|
||||
|
||||
require github.com/stretchr/testify v1.10.0
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
|
|||
10
go.sum
Normal file
10
go.sum
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
Loading…
Add table
Add a link
Reference in a new issue