aoc2024/day-09/internal/two/disk.go

142 lines
3.4 KiB
Go
Raw Normal View History

2024-12-10 21:42:26 +00:00
package two
import (
"cmp"
"container/heap"
"errors"
"math"
"slices"
2024-12-10 21:42:26 +00:00
log "github.com/sirupsen/logrus"
)
const numEmptyBlocks int = 9
2024-12-10 21:42:26 +00:00
type disk struct {
data []int
fileblocks []*block
emptyblocks [numEmptyBlocks]minHeap
2024-12-10 21:42:26 +00:00
}
func newDisk(raw []int) *disk {
fileblocks := make([]*block, 0)
emptyblockHeaps := make([]minHeap, numEmptyBlocks)
for i := range numEmptyBlocks {
heap.Init(&emptyblockHeaps[i])
}
var sz int
for i, id := 0, 0; i < len(raw); id++ {
sz = 0
for j := i; j < len(raw) && raw[j] == id; j++ {
sz++
2024-12-10 21:42:26 +00:00
}
fileblocks = append(fileblocks, newBlock(kindOfFile, id, i, sz, id))
i += sz
sz = 0
for j := i; j < len(raw) && raw[j] == empty; j++ {
sz++
}
if sz > 0 {
heap.Push(&emptyblockHeaps[sz-1], newBlock(kindOfEmpty, id, i, sz, empty))
2024-12-10 21:42:26 +00:00
}
i += sz
2024-12-10 21:42:26 +00:00
}
log.Debugf("\n%v\n%v", fileblocks, emptyblockHeaps)
return &disk{data: raw, fileblocks: fileblocks, emptyblocks: [numEmptyBlocks]minHeap(emptyblockHeaps)}
}
2024-12-10 21:42:26 +00:00
func (d *disk) defragment() {
for i := len(d.fileblocks) - 1; i >= 0; i-- {
log.Debugf("searching for space for fileblock %d: %v", i, d.fileblocks[i])
2024-12-10 21:42:26 +00:00
emptyblock, err := d.getNextEmptyBlock(d.fileblocks[i])
if err != nil {
log.Debug(err)
continue
}
2024-12-10 21:42:26 +00:00
oldStart := d.fileblocks[i].start
oldLength := d.fileblocks[i].length
2024-12-10 21:42:26 +00:00
// we've found an appropriate empty block, now swap the data
d.fileblocks[i].start, emptyblock.start = emptyblock.start, emptyblock.start+d.fileblocks[i].length
2024-12-10 21:42:26 +00:00
// push the resized empty block to a new heap
emptyblock.length -= d.fileblocks[i].length
if emptyblock.length > 0 {
log.Debugf("emptyblock resized %d", emptyblock.length)
heap.Push(&d.emptyblocks[emptyblock.length-1], emptyblock)
2024-12-10 21:42:26 +00:00
}
// now create a new empty block and push it to the appropriate heap
heap.Push(
&d.emptyblocks[oldLength-1],
newBlock(kindOfEmpty, math.MaxInt, oldStart, oldLength, empty),
)
2024-12-10 21:42:26 +00:00
}
}
func (d *disk) getNextEmptyBlock(currentFile *block) (*block, error) {
// collect all minblocks the same size as the current file or greater
minBlocks := []*block{}
for j := currentFile.length; j <= numEmptyBlocks; j++ {
if d.emptyblocks[j-1].Len() == 0 {
continue
}
currentblock := heap.Pop(&d.emptyblocks[j-1]).(*block)
minBlocks = append(minBlocks, currentblock)
2024-12-10 21:42:26 +00:00
}
if len(minBlocks) == 0 {
return nil, errors.New("no empty blocks found")
2024-12-10 21:42:26 +00:00
}
// sort the blocks by id
slices.SortFunc(minBlocks, func(a, b *block) int {
return cmp.Compare(a.id, b.id)
})
// push back the ones we won't be using
for _, block := range minBlocks[1:] {
heap.Push(&d.emptyblocks[block.length-1], block)
2024-12-10 21:42:26 +00:00
}
// if the lowest id minblock is positioned after the current file, push it back and return an error
if minBlocks[0].start >= currentFile.start {
heap.Push(&d.emptyblocks[minBlocks[0].length-1], minBlocks[0])
return nil, errors.New("no empty blocks found")
}
log.Debugf("found empty space %v", minBlocks[0])
return minBlocks[0], nil
2024-12-10 21:42:26 +00:00
}
func (d *disk) write() {
allBlocks := make([]*block, 0)
allBlocks = append(allBlocks, d.fileblocks...)
for i := 0; i < numEmptyBlocks; i++ {
for range d.emptyblocks[i].Len() {
allBlocks = append(allBlocks, heap.Pop(&d.emptyblocks[i]).(*block))
}
}
slices.SortFunc(allBlocks, func(a, b *block) int {
return cmp.Compare(a.start, b.start)
})
for _, block := range allBlocks {
for i := block.start; i < block.start+block.length; i++ {
d.data[i] = block.value
2024-12-10 21:42:26 +00:00
}
}
}