mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
39 lines
589 B
Go
39 lines
589 B
Go
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
|
|
}
|