move file parsing into util.go

This commit is contained in:
onyx-and-iris 2024-12-19 17:58:45 +00:00
parent f6eeece8d7
commit a4e8e83844
4 changed files with 92 additions and 84 deletions

View File

@ -1,11 +1,7 @@
package one
import (
"bufio"
"bytes"
"io"
"slices"
"strconv"
)
const empty = -1
@ -30,41 +26,3 @@ func Solve(buf []byte) (int, error) {
return checksum, nil
}
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
}

View File

@ -0,0 +1,46 @@
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
}

View File

@ -1,11 +1,7 @@
package two
import (
"bufio"
"bytes"
"io"
"slices"
"strconv"
)
const empty = -1
@ -32,41 +28,3 @@ func Solve(buf []byte) (int, error) {
return sum, nil
}
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
}

View File

@ -0,0 +1,46 @@
package two
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
}