From abf7abf3ea66e2d5ad63588ef7d8005717bad5de Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 19 Dec 2024 20:32:42 +0000 Subject: [PATCH] add {heap}.IsEmpty() and use it to empty the block during write() --- day-09/internal/two/disk.go | 2 +- day-09/internal/two/heap.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/day-09/internal/two/disk.go b/day-09/internal/two/disk.go index 1c52ac8..037b8e1 100644 --- a/day-09/internal/two/disk.go +++ b/day-09/internal/two/disk.go @@ -124,7 +124,7 @@ func (d *disk) write() { allBlocks = append(allBlocks, d.fileblocks...) for i := 0; i < numEmptyBlocks; i++ { - for range d.emptyblocks[i].Len() { + for !d.emptyblocks[i].IsEmpty() { allBlocks = append(allBlocks, heap.Pop(&d.emptyblocks[i]).(*block)) } } diff --git a/day-09/internal/two/heap.go b/day-09/internal/two/heap.go index db5cba8..6e68e37 100644 --- a/day-09/internal/two/heap.go +++ b/day-09/internal/two/heap.go @@ -25,3 +25,7 @@ func (h *minHeap) Pop() interface{} { *h = old[:n-1] return x } + +func (h *minHeap) IsEmpty() bool { + return h.Len() == 0 +}