chore: imporivements
Some checks failed
Go Bump / bump (push) Successful in 6s
Go Test / build (push) Failing after 29s

This commit is contained in:
xuu
2023-12-13 08:32:37 -07:00
parent 127e9c33c6
commit 927fabebfc
9 changed files with 145 additions and 138 deletions

View File

@@ -7,27 +7,19 @@ import (
"sort"
"strconv"
"strings"
aoc "go.sour.is/advent-of-code-2023"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Usage: day05 FILE")
}
func main() { aoc.MustResult(aoc.Runner(run)) }
input, err := os.Open(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
scan := bufio.NewScanner(input)
minLocation, minRangeLocation := run(scan)
fmt.Println("min location:", minLocation)
fmt.Println("min range location:", minRangeLocation)
type result struct {
minLocation int
minRange int
}
func run(scan *bufio.Scanner) (int, int) {
func run(scan *bufio.Scanner) (result, error) {
log("begin...")
var seeds []int
@@ -55,15 +47,14 @@ func run(scan *bufio.Scanner) (int, int) {
lookup["humidity-to-location"],
)
return findMinLocation(seeds, find), FindMinRangeLocationMulti(seedRanges, find)
return result{findMinLocation(seeds, find), FindMinRangeLocationMulti(seedRanges, find)}, nil
}
func readSeeds(text string) ([]int, [][2]int) {
var seeds [] int
var seeds []int
var seedRanges [][2]int
sp := strings.Fields(strings.TrimPrefix(text, "seeds: "))
for i, s := range sp {
n, _ := strconv.Atoi(s)
for i, n := range aoc.SliceMap(aoc.Atoi, strings.Fields(strings.TrimPrefix(text, "seeds: "))...) {
seeds = append(seeds, n)
if i%2 == 0 {
@@ -121,7 +112,7 @@ func FindMinRangeLocation(ranges [][2]int, find *Finder) int {
for _, s := range ranges {
for i := 0; i < s[1]; i++ {
seedLocations = append(seedLocations, find.Find(s[0] + i))
seedLocations = append(seedLocations, find.Find(s[0]+i))
}
}
return min(seedLocations...)
@@ -164,7 +155,7 @@ func FindMinRangeLocationMulti(ranges [][2]int, find *Finder) int {
seedLocations := make([]int, 0, results)
expectResults := make([]struct{}, len(ranges))
for range expectResults {
r := <- resultsCh
r := <-resultsCh
seedLocations = append(seedLocations, r...)
}