From be12e11f5e231d02a4e35f74918f0449e5f60c38 Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Mon, 4 Dec 2023 23:54:13 +0100 Subject: [PATCH 1/2] Non-working code, archival purpose --- day03/engine-schema.go | 69 +++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/day03/engine-schema.go b/day03/engine-schema.go index 374be9a..e52af67 100644 --- a/day03/engine-schema.go +++ b/day03/engine-schema.go @@ -19,39 +19,51 @@ func PrintAndWait(x ...any) { fmt.Print(x) fmt.Scanln() } -// ScanTwoLines takes `lines` and `index` to scan lines[index] -// and lines[index + 1] -func ScanTwoLines(lines []string, index int) { - + +func MatchNumbers(lines []string, index int, ast []int) { + // Regex that finds the numbers in a row renum := regexp.MustCompile("[0-9]+") - resym := regexp.MustCompile("[^0-9.]+") - firstLineNums := renum.FindAllStringIndex(lines[index], -1) - firstLineSymbolsIndex := resym.FindAllStringIndex(lines[index], -1) - secondLineSymbolsIndex := resym.FindAllStringIndex(lines[index + 1], -1) + // Gather numbers in prev line + prevLine := renum.FindAllStringIndex(lines[index-1], -1) + // Gather numbers in this line + thisLine := renum.FindAllStringIndex(lines[index], -1) + // Gather numbers in next line + nextLine := renum.FindAllStringIndex(lines[index+1], -1) + // Calculate the number of numbers in three lines + totalNumbers := len(prevLine) + len(thisLine) + len(nextLine) + + PrintAndWait(ast, "\n", prevLine, "\n", thisLine, "\n", nextLine) + PrintAndWait(totalNumbers) + + matches := 0 + twoNums := [2]int{0, 0} + for i := 0; i < totalNumbers && matches < 2; i++ { + if ast[0] < + } +} + +func CheckGears(lines []string) { + + // Regex that finds the numbers in a row + //renum := regexp.MustCompile("[0-9]+") + // Regex that finds the asterisks in a row + resym := regexp.MustCompile("[*]") + // For every line starting from the second + for i := 0; i < len(lines) - 1; i++ { + // Take the index of the asterisks + asteriskIndex := resym.FindAllStringIndex(lines[i], - 1) + // For every index we get + for j := range asteriskIndex { + MatchNumbers(lines, i, asteriskIndex[j]) + } + } + // firstLineNums := renum.FindAllStringIndex(lines[index], -1) + // firstLineSymbolsIndex := resym.FindAllStringIndex(lines[index], -1) + // secondLineSymbolsIndex := resym.FindAllStringIndex(lines[index + 1], -1) // For every *number index range*, check if in the same line there is a // symbol on (first - 1) or (last + 1), check in other lines if there is // a symbol in a specific interval of numbers. If you find a match, you // can break as you just need one symbol - for i := range firstLineNums { - for j := range firstLineSymbolsIndex { - if firstLineSymbolsIndex[j][index] >= firstLineNums[i][index] - 1 && - (firstLineSymbolsIndex[j][index] <= firstLineNums[i][index + 1]) { - totalSum += numbers[index][i] - break - } - } - for j := range secondLineSymbolsIndex { - if (secondLineSymbolsIndex[j][index] >= firstLineNums[i][index] - 1) && - (secondLineSymbolsIndex[j][index] <= firstLineNums[i][index + 1]) { - totalSum += numbers[index][i] - break - } - } - } -} - -func ScanThreeLines() { - } func main() { @@ -173,4 +185,5 @@ OuterLoop: } } fmt.Printf("The total sum is: %d\n", totalSum) + CheckGears(lines) } From 8c6d00683e34f5853ce357b484707113f0f084a8 Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Tue, 5 Dec 2023 20:13:44 +0100 Subject: [PATCH 2/2] Working version of d3p2, code needs refactoring --- day03/engine-schema.go | 43 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/day03/engine-schema.go b/day03/engine-schema.go index e52af67..ccac1c6 100644 --- a/day03/engine-schema.go +++ b/day03/engine-schema.go @@ -20,7 +20,7 @@ func PrintAndWait(x ...any) { fmt.Scanln() } -func MatchNumbers(lines []string, index int, ast []int) { +func MatchNumbers(lines []string, index int, ast []int, total *int) { // Regex that finds the numbers in a row renum := regexp.MustCompile("[0-9]+") // Gather numbers in prev line @@ -32,18 +32,48 @@ func MatchNumbers(lines []string, index int, ast []int) { // Calculate the number of numbers in three lines totalNumbers := len(prevLine) + len(thisLine) + len(nextLine) - PrintAndWait(ast, "\n", prevLine, "\n", thisLine, "\n", nextLine) - PrintAndWait(totalNumbers) + // Now we create a big array with all the indexes + allIndexes := prevLine + for i := range thisLine { + allIndexes = append(allIndexes, thisLine[i]) + } + for i := range nextLine { + allIndexes = append(allIndexes, nextLine[i]) + } + // Now we create a big array with all the numbers + // We start from the previous line + allNumbers := renum.FindAllString(lines[index-1], -1) + thisNums := renum.FindAllString(lines[index], -1) + for i := range thisNums { + allNumbers = append(allNumbers, thisNums[i]) + } + nextNums := renum.FindAllString(lines[index+1], -1) + for i := range nextNums { + allNumbers = append(allNumbers, nextNums[i]) + } + // When we start, we have zero matches matches := 0 + // We will stop when we encounter two numbers twoNums := [2]int{0, 0} + _ = twoNums + // Cycling through all numbers, but stopping at two matches for i := 0; i < totalNumbers && matches < 2; i++ { - if ast[0] < + if (ast[0] >= allIndexes[i][0] - 1 && ast[0] <= allIndexes[i][1]) { + matches += 1 + num, _ := strconv.Atoi(allNumbers[i]) + twoNums[matches - 1] = num + } + } + if(matches == 2) { + tempGears := twoNums[0] * twoNums[1] + *total += tempGears } } func CheckGears(lines []string) { - + total := 0 + totalPoint := &total // Regex that finds the numbers in a row //renum := regexp.MustCompile("[0-9]+") // Regex that finds the asterisks in a row @@ -54,7 +84,7 @@ func CheckGears(lines []string) { asteriskIndex := resym.FindAllStringIndex(lines[i], - 1) // For every index we get for j := range asteriskIndex { - MatchNumbers(lines, i, asteriskIndex[j]) + MatchNumbers(lines, i, asteriskIndex[j], totalPoint) } } // firstLineNums := renum.FindAllStringIndex(lines[index], -1) @@ -64,6 +94,7 @@ func CheckGears(lines []string) { // symbol on (first - 1) or (last + 1), check in other lines if there is // a symbol in a specific interval of numbers. If you find a match, you // can break as you just need one symbol + fmt.Printf("Total of gears is: %d\n", total) } func main() {