From a495a2ef1a57364daefcc4e7bbefd81b2655b16d Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 9 Dec 2024 12:49:56 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20Blocks=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blocks.go | 27 ++++++++++++++++++++++++++- cmd/defrag/main.go | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/blocks.go b/blocks.go index 8b3ba09..2c90f7a 100644 --- a/blocks.go +++ b/blocks.go @@ -1,5 +1,30 @@ package defrag +import ( + "bytes" + "strconv" + "strings" +) + 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 } diff --git a/cmd/defrag/main.go b/cmd/defrag/main.go index e620e91..99bdecf 100644 --- a/cmd/defrag/main.go +++ b/cmd/defrag/main.go @@ -1,10 +1,10 @@ package main import ( + "bytes" "fmt" "io" "os" - "bytes" "codeberg.org/danjones000/advent-of-code/2024-09/defrag" )