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

31 lines
370 B
Go

package two
import (
"bytes"
)
const empty = -1
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
expandedRaw, err := parseLines(r)
if err != nil {
return 0, err
}
disk := newDisk(expandedRaw)
disk.defragment()
disk.write()
var sum int
for i, n := range disk.data {
if n == empty {
continue
}
sum += i * n
}
return sum, nil
}