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

29 lines
367 B
Go

package one
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.reorganise()
var checksum int
for i, n := range disk.data {
if n == empty {
break
}
checksum += i * n
}
return checksum, nil
}