mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
remove internal/util
This commit is contained in:
parent
c70d4a74cf
commit
064232967b
@ -3,13 +3,11 @@ package one
|
||||
import (
|
||||
"bytes"
|
||||
"slices"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-05/internal/util"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
updates, orderings, err := util.ParseLines(r)
|
||||
updates, orderings, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package util
|
||||
package one
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@ -9,7 +9,7 @@ import (
|
||||
"github.com/onyx-and-iris/aoc2024/day-05/internal/update"
|
||||
)
|
||||
|
||||
func ParseLines(r io.Reader) ([]update.Update, map[int][]int, error) {
|
||||
func parseLines(r io.Reader) ([]update.Update, map[int][]int, error) {
|
||||
var updates []update.Update
|
||||
orderings := make(map[int][]int, 0)
|
||||
|
@ -3,13 +3,11 @@ package two
|
||||
import (
|
||||
"bytes"
|
||||
"slices"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-05/internal/util"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
updates, orderings, err := util.ParseLines(r)
|
||||
updates, orderings, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
53
day-05/internal/two/util.go
Normal file
53
day-05/internal/two/util.go
Normal file
@ -0,0 +1,53 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-05/internal/update"
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) ([]update.Update, map[int][]int, error) {
|
||||
var updates []update.Update
|
||||
orderings := make(map[int][]int, 0)
|
||||
|
||||
var inUpdates bool
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if len(line) == 0 {
|
||||
inUpdates = true
|
||||
continue
|
||||
}
|
||||
|
||||
if inUpdates {
|
||||
var update update.Update
|
||||
for _, n := range strings.Split(line, ",") {
|
||||
update.Pages = append(update.Pages, mustConv(n))
|
||||
}
|
||||
updates = append(updates, update)
|
||||
} else {
|
||||
left, right := func() (int, int) {
|
||||
x := strings.Split(line, "|")
|
||||
return mustConv(x[0]), mustConv(x[1])
|
||||
}()
|
||||
orderings[left] = append(orderings[left], right)
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return updates, orderings, nil
|
||||
}
|
||||
|
||||
func mustConv(s string) int {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
Loading…
Reference in New Issue
Block a user