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