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

39 lines
589 B
Go
Raw Normal View History

2024-12-10 21:42:26 +00:00
package one
import (
"bufio"
"io"
"strconv"
)
func parseLines(r io.Reader) ([]block, error) {
var blocks []block
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
for i := 0; i < len(line); i += 2 {
var free int
if i < len(line)-1 {
free = mustConv(string(line[i+1]))
}
blocks = append(blocks, block{mustConv(string(line[i])), free})
}
}
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
}