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

29 lines
367 B
Go
Raw Normal View History

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