From e5c255d75905d1eef14e96c467cfa6e89ba8e4e8 Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Sun, 3 Dec 2023 13:24:33 +0000 Subject: [PATCH 1/5] Initial draft of day03, non-working, archival purpose --- day03/engine-schema.go | 68 ++++++++++++++++++++++++++++++++++++++++++ day03/inputs | 1 + 2 files changed, 69 insertions(+) create mode 100644 day03/engine-schema.go create mode 120000 day03/inputs diff --git a/day03/engine-schema.go b/day03/engine-schema.go new file mode 100644 index 0000000..dc7cc7b --- /dev/null +++ b/day03/engine-schema.go @@ -0,0 +1,68 @@ + +package main + +import ( + "fmt" +// "strings" + "bufio" + "os" + "regexp" + "strconv" +) + +func check(e error) { + if e != nil { + panic(e) + } +} + +func PrintAndWait[T any](x T) { + fmt.Print(x) + fmt.Scanln() +} + +func main() { + file, err := os.Open("./inputs/day03_test_input") + check(err) + defer file.Close() + + // This regex find the numbers inside strings + renum := regexp.MustCompile("[0-9]+") + resym := regexp.MustCompile("[^0-9.]+") + + scanner := bufio.NewScanner(file) + var lines []string + + // Read the whole file into an array of strings + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + numLines := len(lines) + + // The 2D array of numbers will hold all the numbers to easily + // match them with corresponding strings in the file using the index + numbers := make([][]int, numLines) + // For every line in the file, cerate an array of numbers + for i := 0; i < numLines; i++ { + tempNums := renum.FindAllString(lines[i], -1) + for j := 0; j < len(tempNums); j++ { + num, _ := strconv.Atoi(tempNums[j]) + numbers[i] = append(numbers[i], num) + } + } + + // We store the index of a symbol on the line it appears + symbolsIndex := make([][]int, numLines) + symbols := make([][]string, numLines) + _ = symbolsIndex + // For every line + for i := 0; i < numLines; i++ { + // We put all the symbols in a string + tempSymbols := resym.FindAllString(lines[i], -1) + // We associate symbols with an index + tempSymbolsIndex := resym.FindAllStringIndex(lines[i], -1) + // Add symbols and indexes to the arrays + symbols = append(symbols, tempSymbols) + //symbolsIndex = append(symbolsIndex, tempSymbolsIndex) + } +} diff --git a/day03/inputs b/day03/inputs new file mode 120000 index 0000000..a80bb2b --- /dev/null +++ b/day03/inputs @@ -0,0 +1 @@ +../inputs \ No newline at end of file From 9925c227a4e5538b74b564d7f7921afbd41ec51b Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Sun, 3 Dec 2023 16:10:23 +0000 Subject: [PATCH 2/5] Scrapping the current approach. Archival purpose --- day03/engine-schema.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/day03/engine-schema.go b/day03/engine-schema.go index dc7cc7b..7c54951 100644 --- a/day03/engine-schema.go +++ b/day03/engine-schema.go @@ -16,7 +16,7 @@ func check(e error) { } } -func PrintAndWait[T any](x T) { +func PrintAndWait(x ...any) { fmt.Print(x) fmt.Scanln() } @@ -61,8 +61,26 @@ func main() { tempSymbols := resym.FindAllString(lines[i], -1) // We associate symbols with an index tempSymbolsIndex := resym.FindAllStringIndex(lines[i], -1) + PrintAndWait(i, "First loop.") + // A line can contain 0 symbols + if len(tempSymbols) == 0 { + symbols[i] = make([]string, 0) + symbolsIndex[i] = make([]int, 0) + } // Add symbols and indexes to the arrays - symbols = append(symbols, tempSymbols) + for j := 0; j < len(tempSymbols); j++ { + PrintAndWait(i, j, "Second loop.") + symbols[i] = append(symbols[i], tempSymbols[j]) + } + for j:= 0; j < len(tempSymbolsIndex); j++ { + PrintAndWait(i, j, "Third loop.") + PrintAndWait(tempSymbolsIndex) + symbolsIndex[i] = append(symbolsIndex[i], tempSymbolsIndex[j][0]) + symbolsIndex[i] = append(symbolsIndex[i], tempSymbolsIndex[j][1]) + } + PrintAndWait(symbols) + PrintAndWait(symbolsIndex) //symbolsIndex = append(symbolsIndex, tempSymbolsIndex) } + PrintAndWait(numbers) } From 8c6b9b3dfe0ef35ea3c15a350649963963bb583a Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Sun, 3 Dec 2023 16:52:32 +0000 Subject: [PATCH 3/5] WIP of new approach --- day03/engine-schema.go | 56 ++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/day03/engine-schema.go b/day03/engine-schema.go index 7c54951..5a15921 100644 --- a/day03/engine-schema.go +++ b/day03/engine-schema.go @@ -7,7 +7,7 @@ import ( "bufio" "os" "regexp" - "strconv" +// "strconv" ) func check(e error) { @@ -22,7 +22,7 @@ func PrintAndWait(x ...any) { } func main() { - file, err := os.Open("./inputs/day03_test_input") + file, err := os.Open("./inputs/day03_input") check(err) defer file.Close() @@ -38,7 +38,25 @@ func main() { lines = append(lines, scanner.Text()) } numLines := len(lines) + numbers := make([][]int, numLines) + _ = numbers + /* Line [0] needs to check only line 1 for symbols. + Similarly, the last line needs to check only + (last line index - 1) for symbols. + So we first check the line [0], then start a loop + from 1 to (last line index - 1) + */ + firstLineNums := renum.FindAllStringIndex(lines[0], -1) + firstLineSymbolsIndex := resym.FindAllStringIndex(lines[0], -1) + secondLineSymbolsIndex := resym.FindAllStringIndex(lines[1], -1) + + PrintAndWait(firstLineNums) + PrintAndWait(firstLineSymbolsIndex) + PrintAndWait(secondLineSymbolsIndex) + + + /* This code may need to be scrapped, not sure yet. // The 2D array of numbers will hold all the numbers to easily // match them with corresponding strings in the file using the index numbers := make([][]int, numLines) @@ -50,37 +68,5 @@ func main() { numbers[i] = append(numbers[i], num) } } - - // We store the index of a symbol on the line it appears - symbolsIndex := make([][]int, numLines) - symbols := make([][]string, numLines) - _ = symbolsIndex - // For every line - for i := 0; i < numLines; i++ { - // We put all the symbols in a string - tempSymbols := resym.FindAllString(lines[i], -1) - // We associate symbols with an index - tempSymbolsIndex := resym.FindAllStringIndex(lines[i], -1) - PrintAndWait(i, "First loop.") - // A line can contain 0 symbols - if len(tempSymbols) == 0 { - symbols[i] = make([]string, 0) - symbolsIndex[i] = make([]int, 0) - } - // Add symbols and indexes to the arrays - for j := 0; j < len(tempSymbols); j++ { - PrintAndWait(i, j, "Second loop.") - symbols[i] = append(symbols[i], tempSymbols[j]) - } - for j:= 0; j < len(tempSymbolsIndex); j++ { - PrintAndWait(i, j, "Third loop.") - PrintAndWait(tempSymbolsIndex) - symbolsIndex[i] = append(symbolsIndex[i], tempSymbolsIndex[j][0]) - symbolsIndex[i] = append(symbolsIndex[i], tempSymbolsIndex[j][1]) - } - PrintAndWait(symbols) - PrintAndWait(symbolsIndex) - //symbolsIndex = append(symbolsIndex, tempSymbolsIndex) - } - PrintAndWait(numbers) + */ } From 1c2e835ebcf405d15853fccc311a920cff549dbd Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Sun, 3 Dec 2023 21:33:46 +0100 Subject: [PATCH 4/5] Working code, terrible code, debug prints --- day03/engine-schema.go | 128 ++++++++++++++++++++++++++++++++++------- 1 file changed, 108 insertions(+), 20 deletions(-) diff --git a/day03/engine-schema.go b/day03/engine-schema.go index 5a15921..bf9ac47 100644 --- a/day03/engine-schema.go +++ b/day03/engine-schema.go @@ -1,4 +1,3 @@ - package main import ( @@ -7,7 +6,7 @@ import ( "bufio" "os" "regexp" -// "strconv" + "strconv" ) func check(e error) { @@ -32,6 +31,7 @@ func main() { scanner := bufio.NewScanner(file) var lines []string + var totalSum int = 0 // Read the whole file into an array of strings for scanner.Scan() { @@ -39,8 +39,17 @@ func main() { } numLines := len(lines) numbers := make([][]int, numLines) - _ = numbers + // The 2D array of numbers will hold all the numbers to easily + // match them with corresponding strings in the file using the index + // For every line in the file, cerate an array of numbers + for i := 0; i < numLines; i++ { + tempNums := renum.FindAllString(lines[i], -1) + for j := 0; j < len(tempNums); j++ { + num, _ := strconv.Atoi(tempNums[j]) + numbers[i] = append(numbers[i], num) + } + } /* Line [0] needs to check only line 1 for symbols. Similarly, the last line needs to check only (last line index - 1) for symbols. @@ -50,23 +59,102 @@ func main() { firstLineNums := renum.FindAllStringIndex(lines[0], -1) firstLineSymbolsIndex := resym.FindAllStringIndex(lines[0], -1) secondLineSymbolsIndex := resym.FindAllStringIndex(lines[1], -1) - - PrintAndWait(firstLineNums) - PrintAndWait(firstLineSymbolsIndex) - PrintAndWait(secondLineSymbolsIndex) - - - /* This code may need to be scrapped, not sure yet. - // The 2D array of numbers will hold all the numbers to easily - // match them with corresponding strings in the file using the index - numbers := make([][]int, numLines) - // For every line in the file, cerate an array of numbers - for i := 0; i < numLines; i++ { - tempNums := renum.FindAllString(lines[i], -1) - for j := 0; j < len(tempNums); j++ { - num, _ := strconv.Atoi(tempNums[j]) - numbers[i] = append(numbers[i], num) + // 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][0] >= firstLineNums[i][0] - 1 && + (firstLineSymbolsIndex[j][0] <= firstLineNums[i][1]) { + totalSum += numbers[0][i] + break + } + } + for j := range secondLineSymbolsIndex { + if (secondLineSymbolsIndex[j][0] >= firstLineNums[i][0] - 1) && + (secondLineSymbolsIndex[j][0] <= firstLineNums[i][1]) { + totalSum += numbers[0][i] + break + } } } - */ + PrintAndWait(totalSum) + // Now we loop from 1 to (last index - i) + for i := 1; i < len(lines) - 1; i++ { + // We need to check the current line against an interval of three lines + // breaking the loop for a single number as soon as we find a match + // (we don't want duplicate matches) + currentLineNums := renum.FindAllStringIndex(lines[i], -1) + previousLineIndex := resym.FindAllStringIndex(lines[i - 1], -1) + currentLineIndex := resym.FindAllStringIndex(lines[i], -1) + nextLineIndex := resym.FindAllStringIndex(lines[i + 1], -1) + PrintAndWait("i: ", i) +OuterLoop: + for k := range currentLineNums { + PrintAndWait("k: ", k, currentLineNums) + for j := range previousLineIndex { + PrintAndWait("prev j: ", j, previousLineIndex) + if previousLineIndex[j][0] >= currentLineNums[k][0] - 1 && + previousLineIndex[j][0] <= currentLineNums[k][1] { + PrintAndWait(numbers[i][k]) + totalSum += numbers[i][k] + continue OuterLoop + } + } + for j := range currentLineIndex { + PrintAndWait("cur j: ", j, currentLineIndex) + if currentLineIndex[j][0] >= currentLineNums[k][0] - 1 && + currentLineIndex[j][0] <= currentLineNums[k][1] { + PrintAndWait(numbers[i][k]) + totalSum += numbers[i][k] + continue OuterLoop + } + } + for j := range nextLineIndex { + PrintAndWait("next j: ", j, nextLineIndex) + if nextLineIndex[j][0] >= currentLineNums[k][0] - 1 && + nextLineIndex[j][0] <= currentLineNums[k][1] { + PrintAndWait(numbers[i][k]) + totalSum += numbers[i][k] + continue OuterLoop + } + } + } + } + // Now we need to loop the last line and confront it with previous + // and itself + lastLineNums := renum.FindAllStringIndex(lines[len(lines) - 1], -1) + lastLineSymbolsIndex := resym.FindAllStringIndex(lines[len(lines) - 1], -1) + notLastLineSymbolsIndex := resym.FindAllStringIndex(lines[len(lines) - 2], -1) + // For every *number index range*, check if in the same line there is a + // symbol on (last - 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 lastLineNums { + PrintAndWait("i: ", i, lastLineNums) + for j := range lastLineSymbolsIndex { + PrintAndWait("last j: ", j, notLastLineSymbolsIndex) + if lastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1 && + (lastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) { + PrintAndWait(numbers[len(lines) - 1][i]) + PrintAndWait(totalSum) + totalSum += numbers[len(lines) - 1][i] + PrintAndWait(totalSum) + break + } + } + for j := range notLastLineSymbolsIndex { + PrintAndWait("notlast j: ", j, notLastLineSymbolsIndex) + if (notLastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1) && + (notLastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) { + PrintAndWait(numbers[len(lines) - 1][i]) + PrintAndWait(totalSum) + totalSum += numbers[len(lines) - 1][i] + PrintAndWait(totalSum) + break + } + } + } + fmt.Printf("The total sum is: %d\n", totalSum) } From d11426e518c4787d57f38d5138318335ae317f2d Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Sun, 3 Dec 2023 21:35:05 +0100 Subject: [PATCH 5/5] The worst code I've ever written, but somehow it works --- day03/engine-schema.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/day03/engine-schema.go b/day03/engine-schema.go index bf9ac47..4d109e1 100644 --- a/day03/engine-schema.go +++ b/day03/engine-schema.go @@ -79,7 +79,6 @@ func main() { } } } - PrintAndWait(totalSum) // Now we loop from 1 to (last index - i) for i := 1; i < len(lines) - 1; i++ { // We need to check the current line against an interval of three lines @@ -89,33 +88,25 @@ func main() { previousLineIndex := resym.FindAllStringIndex(lines[i - 1], -1) currentLineIndex := resym.FindAllStringIndex(lines[i], -1) nextLineIndex := resym.FindAllStringIndex(lines[i + 1], -1) - PrintAndWait("i: ", i) OuterLoop: for k := range currentLineNums { - PrintAndWait("k: ", k, currentLineNums) for j := range previousLineIndex { - PrintAndWait("prev j: ", j, previousLineIndex) if previousLineIndex[j][0] >= currentLineNums[k][0] - 1 && previousLineIndex[j][0] <= currentLineNums[k][1] { - PrintAndWait(numbers[i][k]) totalSum += numbers[i][k] continue OuterLoop } } for j := range currentLineIndex { - PrintAndWait("cur j: ", j, currentLineIndex) if currentLineIndex[j][0] >= currentLineNums[k][0] - 1 && currentLineIndex[j][0] <= currentLineNums[k][1] { - PrintAndWait(numbers[i][k]) totalSum += numbers[i][k] continue OuterLoop } } for j := range nextLineIndex { - PrintAndWait("next j: ", j, nextLineIndex) if nextLineIndex[j][0] >= currentLineNums[k][0] - 1 && nextLineIndex[j][0] <= currentLineNums[k][1] { - PrintAndWait(numbers[i][k]) totalSum += numbers[i][k] continue OuterLoop } @@ -132,26 +123,17 @@ OuterLoop: // 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 lastLineNums { - PrintAndWait("i: ", i, lastLineNums) for j := range lastLineSymbolsIndex { - PrintAndWait("last j: ", j, notLastLineSymbolsIndex) if lastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1 && (lastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) { - PrintAndWait(numbers[len(lines) - 1][i]) - PrintAndWait(totalSum) totalSum += numbers[len(lines) - 1][i] - PrintAndWait(totalSum) break } } for j := range notLastLineSymbolsIndex { - PrintAndWait("notlast j: ", j, notLastLineSymbolsIndex) if (notLastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1) && (notLastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) { - PrintAndWait(numbers[len(lines) - 1][i]) - PrintAndWait(totalSum) totalSum += numbers[len(lines) - 1][i] - PrintAndWait(totalSum) break } }