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

42 lines
644 B
Go
Raw Normal View History

2024-12-10 21:42:26 +00:00
package two
import (
"bufio"
"io"
"strconv"
)
func parseLines(r io.Reader) ([]block, error) {
var blocks []block
var start int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
for i := 0; i < len(line); i += 2 {
used := mustConv(string(line[i]))
start += used
var end int
if i < len(line)-1 {
end = mustConv(string(line[i+1]))
}
blocks = append(blocks, newBlock(used, start, end))
}
}
if err := scanner.Err(); err != nil {
return []block{}, err
}
return blocks, nil
}
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}