From c16d3362fdd9d3b748d46a24eb42275110090dca Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Wed, 6 Dec 2023 16:43:03 +0000 Subject: [PATCH] Implemented function to gather the different items --- day05/seeds.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/day05/seeds.go b/day05/seeds.go index 7845bd6..43861ac 100644 --- a/day05/seeds.go +++ b/day05/seeds.go @@ -20,8 +20,24 @@ func PrintAndWait(x ...any) { fmt.Scanln() } -func GetMaps(ss [][]string, nn [][]int) { - +func GetMaps(scanner *bufio.Scanner, re *regexp.Regexp) [][]int { + // Scan until there is an empty line + var tempNums int = 0 + tempArray := make([][]int, 0) + PrintAndWait(tempArray) + for i := 0; scanner.Scan() && scanner.Text() != ""; i++ { + tempString := re.FindAllString(scanner.Text(), -1) + temp := make([]int, 0) + for j := range tempString { + tempNums, _ = strconv.Atoi(tempString[j]) + temp = append(temp, tempNums) + } + tempArray = append(tempArray, temp) + PrintAndWait(tempArray) + } + // Prepare for next line + scanner.Scan() + return tempArray } func main () { @@ -33,8 +49,8 @@ func main () { renum := regexp.MustCompile("[0-9]+") var seeds []int - //var soils, fertilizers, waters, lights, temperatures, - // humidities, locations [][]int + var soils, fertilizers, waters, lights, temperatures, + humidities, locations [][]int scanner := bufio.NewScanner(file) // We know that the seeds only have one row scanner.Scan() @@ -48,5 +64,12 @@ func main () { // We know we have an empty string and just a title, skip them scanner.Scan() scanner.Scan() - + // Should be possible to just pass the scanner + soils = GetMaps(scanner, renum) + fertilizers = GetMaps(scanner, renum) + waters = GetMaps(scanner, renum) + lights = GetMaps(scanner, renum) + temperatures = GetMaps(scanner, renum) + humidities = GetMaps(scanner, renum) + locations = GetMaps(scanner, renum) }