mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
32 lines
452 B
Go
32 lines
452 B
Go
package two
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func Solve(buf []byte) (int, error) {
|
|
r := bytes.NewReader(buf)
|
|
blocks, err := parseLines(r)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
disk := newDisk(blocks)
|
|
log.Debug(disk.debug())
|
|
disk.sort()
|
|
log.Debug(disk.debug())
|
|
|
|
var checksum int
|
|
for i := 0; i < len(disk.data); i++ {
|
|
if disk.data[i] == empty {
|
|
continue
|
|
}
|
|
|
|
checksum += i * disk.data[i]
|
|
}
|
|
|
|
return checksum, nil
|
|
}
|