mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
42 lines
644 B
Go
42 lines
644 B
Go
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
|
|
}
|