use ParseIntList from common package

This commit is contained in:
Dan Anglin 2023-12-05 20:01:55 +00:00
parent 4b8792fed4
commit bec8830fb6
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

View file

@ -2,9 +2,9 @@ package main
import (
"fmt"
"strconv"
"strings"
"unicode"
"codeflow.dananglin.me.uk/apollo/advent-of-code/internal/common"
)
const (
@ -61,42 +61,16 @@ func parseSeedNums(seedStr string) ([]int, error) {
seedStr = seedStr[strings.Index(seedStr, ":")+1:]
seeds := []int{}
seedNum := ""
addSeed := func(digit string) error {
value, err := strconv.Atoi(digit)
if err != nil {
return fmt.Errorf("unable to convert %q to int; %w", digit, err)
}
seeds = append(seeds, value)
return nil
}
for ind, s := range seedStr {
if unicode.IsDigit(s) {
seedNum = seedNum + string(s)
if ind == len(seedStr)-1 {
if err := addSeed(seedNum); err != nil {
return nil, fmt.Errorf("unable to add %q to seeds; %w", seedNum, err)
}
}
} else {
if len(seedNum) > 0 {
if err := addSeed(seedNum); err != nil {
return nil, fmt.Errorf("unable to add %q to seeds; %w", seedNum, err)
}
}
seedNum = ""
}
seeds, err := common.ParseIntList(seedStr)
if err != nil {
return nil, fmt.Errorf("unable to parse the seed numbers from %q; %w", seedStr, err)
}
return seeds, nil
}
func parseMap(mapLines []string) (map[int]int, error) {
var mapMatrix [][]int
//var mapMatrix [][]int
return nil, nil
}