From a4e8e83844b21d085fa9bdf9cc258a40020e789f Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 19 Dec 2024 17:58:45 +0000 Subject: [PATCH] move file parsing into util.go --- day-09/internal/one/solve.go | 42 -------------------------------- day-09/internal/one/util.go | 46 ++++++++++++++++++++++++++++++++++++ day-09/internal/two/solve.go | 42 -------------------------------- day-09/internal/two/util.go | 46 ++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 84 deletions(-) create mode 100644 day-09/internal/one/util.go create mode 100644 day-09/internal/two/util.go diff --git a/day-09/internal/one/solve.go b/day-09/internal/one/solve.go index 48062ee..692b852 100644 --- a/day-09/internal/one/solve.go +++ b/day-09/internal/one/solve.go @@ -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 -} diff --git a/day-09/internal/one/util.go b/day-09/internal/one/util.go new file mode 100644 index 0000000..e1096b3 --- /dev/null +++ b/day-09/internal/one/util.go @@ -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 +} diff --git a/day-09/internal/two/solve.go b/day-09/internal/two/solve.go index 9958eff..1914a24 100644 --- a/day-09/internal/two/solve.go +++ b/day-09/internal/two/solve.go @@ -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 -} diff --git a/day-09/internal/two/util.go b/day-09/internal/two/util.go new file mode 100644 index 0000000..5e7b3a5 --- /dev/null +++ b/day-09/internal/two/util.go @@ -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 +}