From 495d50ba109499cab7f7306773d2cb0b058d3d3c Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 9 Dec 2024 14:34:01 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Account=20for=20file=20ids=20>?= =?UTF-8?q?=209?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + blocks.go | 17 +++++++++-------- blocks_test.go | 11 +++++------ checksum.go | 14 ++++++++++++++ cmd/defrag/main.go | 9 ++++++++- defrag.go | 6 +++--- defrag_test.go | 19 ------------------- 7 files changed, 40 insertions(+), 37 deletions(-) create mode 100644 checksum.go delete mode 100644 defrag_test.go diff --git a/.gitignore b/.gitignore index 6f72f89..a6782c6 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ go.work.sum # env file .env +defrag diff --git a/blocks.go b/blocks.go index 2c90f7a..20f6051 100644 --- a/blocks.go +++ b/blocks.go @@ -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 } diff --git a/blocks_test.go b/blocks_test.go index 1e1831c..b50bd33 100644 --- a/blocks_test.go +++ b/blocks_test.go @@ -7,16 +7,15 @@ import ( ) type tt struct { - name string - diskMap []byte - blocks []byte - defragged []byte + name string + diskMap []byte + blocks []int } 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..............")}, + {"short", []byte("12345"), []int{0, -1, -1, 1, 1, 1, -1, -1, -1, -1, 2, 2, 2, 2, 2}}, + {"longer", []byte("2333133121414131402"), []int{0, 0, -1, -1, -1, 1, 1, 1, -1, -1, -1, 2, -1, -1, -1, 3, 3, 3, -1, 4, 4, -1, 5, 5, 5, 5, -1, 6, 6, 6, 6, -1, 7, 7, 7, -1, 8, 8, 8, 8, 9, 9}}, } } diff --git a/checksum.go b/checksum.go new file mode 100644 index 0000000..0b934b0 --- /dev/null +++ b/checksum.go @@ -0,0 +1,14 @@ +package defrag + +// import "strconv" + +func Checksum(blocks []int) (sum int, err error) { + for idx, fileByte := range blocks { + if fileByte == -1 { + break + } + + sum += idx * fileByte + } + return +} diff --git a/cmd/defrag/main.go b/cmd/defrag/main.go index 99bdecf..e5e7fd7 100644 --- a/cmd/defrag/main.go +++ b/cmd/defrag/main.go @@ -29,6 +29,7 @@ func main() { blocks, err := defrag.Blocks(input) handleErr(err) + err = defrag.Defrag(blocks) handleErr(err) @@ -36,5 +37,11 @@ func main() { fmt.Printf("%s\n", input) fmt.Println("Got this") - fmt.Printf("%s\n", blocks) + fmt.Printf("%v\n", blocks) + + sum, err := defrag.Checksum(blocks) + handleErr(err) + + fmt.Println("Checksum:") + fmt.Printf("%d\n", sum) } diff --git a/defrag.go b/defrag.go index 82204d2..8623c4f 100644 --- a/defrag.go +++ b/defrag.go @@ -2,18 +2,18 @@ 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 { +func Defrag(blocks []int) error { lastPulled := len(blocks) lastPushed := -1 for block := 0; block < len(blocks); block++ { - if blocks[block] != '.' { + if blocks[block] != -1 { continue } if lastPulled <= block { break } for swap := lastPulled - 1; swap > lastPushed; swap-- { - if blocks[swap] == '.' { + if blocks[swap] == -1 { continue } blocks[block], blocks[swap] = blocks[swap], blocks[block] diff --git a/defrag_test.go b/defrag_test.go deleted file mode 100644 index 62f625b..0000000 --- a/defrag_test.go +++ /dev/null @@ -1,19 +0,0 @@ -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) - }) - } -}