mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
50 lines
812 B
Go
50 lines
812 B
Go
|
package util
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"io"
|
||
|
"log"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func MustConv(s string) int {
|
||
|
n, err := strconv.Atoi(s)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return n
|
||
|
}
|
||
|
|
||
|
func ParseLines(r io.Reader) ([][][]int, error) {
|
||
|
data := make([][][]int, 2)
|
||
|
|
||
|
filter := func(r rune) bool {
|
||
|
return r == '|' || r == ','
|
||
|
}
|
||
|
|
||
|
buf, err := io.ReadAll(r)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
parts := strings.Split(string(buf), "\n\n")
|
||
|
|
||
|
for i, part := range parts {
|
||
|
scanner := bufio.NewScanner(strings.NewReader(part))
|
||
|
for scanner.Scan() {
|
||
|
filtered := strings.FieldsFunc(scanner.Text(), filter)
|
||
|
var nums []int
|
||
|
for _, n := range filtered {
|
||
|
nums = append(nums, MustConv(n))
|
||
|
}
|
||
|
data[i] = append(data[i], nums)
|
||
|
}
|
||
|
|
||
|
if err := scanner.Err(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return data, nil
|
||
|
}
|