diff --git a/blocks.go b/blocks.go new file mode 100644 index 0000000..8b3ba09 --- /dev/null +++ b/blocks.go @@ -0,0 +1,5 @@ +package defrag + +func Blocks(diskMap []byte) ([]byte, error) { + return nil, nil +} diff --git a/blocks_test.go b/blocks_test.go new file mode 100644 index 0000000..1e1831c --- /dev/null +++ b/blocks_test.go @@ -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) + }) + } +} diff --git a/cmd/defrag/main.go b/cmd/defrag/main.go new file mode 100644 index 0000000..29ac7ed --- /dev/null +++ b/cmd/defrag/main.go @@ -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) +} diff --git a/defrag.go b/defrag.go new file mode 100644 index 0000000..82204d2 --- /dev/null +++ b/defrag.go @@ -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 +} diff --git a/defrag_test.go b/defrag_test.go new file mode 100644 index 0000000..62f625b --- /dev/null +++ b/defrag_test.go @@ -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) + }) + } +} diff --git a/go.mod b/go.mod index 8879fe6..610e26e 100644 --- a/go.mod +++ b/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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..713a0b4 --- /dev/null +++ b/go.sum @@ -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=