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

47 lines
808 B
Go
Raw Permalink Normal View History

2024-12-19 17:58:45 +00:00
package one
import (
"bufio"
"io"
"slices"
"strconv"
)
func parseLines(r io.Reader) ([]int, error) {
var line string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line = scanner.Text()
}
if err := scanner.Err(); err != nil {
return nil, err
}
raw := [][]int{}
for i, id := 0, 0; i < len(line); i, id = i+2, id+1 {
raw = append(raw, []int{id, mustConv(string(line[i]))})
var free int
if i < len(line)-1 {
free = mustConv(string(line[i+1]))
}
raw = append(raw, []int{empty, free})
}
expandedRaw := []int{}
for _, vals := range raw {
segment := slices.Repeat([]int{vals[0]}, vals[1])
expandedRaw = append(expandedRaw, segment...)
}
return expandedRaw, nil
}
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}