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

31 lines
370 B
Go
Raw Permalink Normal View History

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