Compare commits
2 Commits
fa9dde7917
...
4d34f508d5
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d34f508d5 | |||
| eceddf1284 |
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -7,7 +6,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
// "strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func check(e error) {
|
func check(e error) {
|
||||||
@ -32,6 +31,7 @@ func main() {
|
|||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
var lines []string
|
var lines []string
|
||||||
|
var totalSum int = 0
|
||||||
|
|
||||||
// Read the whole file into an array of strings
|
// Read the whole file into an array of strings
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@ -39,8 +39,17 @@ func main() {
|
|||||||
}
|
}
|
||||||
numLines := len(lines)
|
numLines := len(lines)
|
||||||
numbers := make([][]int, numLines)
|
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.
|
/* Line [0] needs to check only line 1 for symbols.
|
||||||
Similarly, the last line needs to check only
|
Similarly, the last line needs to check only
|
||||||
(last line index - 1) for symbols.
|
(last line index - 1) for symbols.
|
||||||
@ -50,23 +59,84 @@ func main() {
|
|||||||
firstLineNums := renum.FindAllStringIndex(lines[0], -1)
|
firstLineNums := renum.FindAllStringIndex(lines[0], -1)
|
||||||
firstLineSymbolsIndex := resym.FindAllStringIndex(lines[0], -1)
|
firstLineSymbolsIndex := resym.FindAllStringIndex(lines[0], -1)
|
||||||
secondLineSymbolsIndex := resym.FindAllStringIndex(lines[1], -1)
|
secondLineSymbolsIndex := resym.FindAllStringIndex(lines[1], -1)
|
||||||
|
// For every *number index range*, check if in the same line there is a
|
||||||
PrintAndWait(firstLineNums)
|
// symbol on (first - 1) or (last + 1), check in other lines if there is
|
||||||
PrintAndWait(firstLineSymbolsIndex)
|
// a symbol in a specific interval of numbers. If you find a match, you
|
||||||
PrintAndWait(secondLineSymbolsIndex)
|
// can break as you just need one symbol
|
||||||
|
for i := range firstLineNums {
|
||||||
|
for j := range firstLineSymbolsIndex {
|
||||||
/* This code may need to be scrapped, not sure yet.
|
if firstLineSymbolsIndex[j][0] >= firstLineNums[i][0] - 1 &&
|
||||||
// The 2D array of numbers will hold all the numbers to easily
|
(firstLineSymbolsIndex[j][0] <= firstLineNums[i][1]) {
|
||||||
// match them with corresponding strings in the file using the index
|
totalSum += numbers[0][i]
|
||||||
numbers := make([][]int, numLines)
|
break
|
||||||
// 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 := range secondLineSymbolsIndex {
|
||||||
for j := 0; j < len(tempNums); j++ {
|
if (secondLineSymbolsIndex[j][0] >= firstLineNums[i][0] - 1) &&
|
||||||
num, _ := strconv.Atoi(tempNums[j])
|
(secondLineSymbolsIndex[j][0] <= firstLineNums[i][1]) {
|
||||||
numbers[i] = append(numbers[i], num)
|
totalSum += numbers[0][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
// 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)
|
||||||
|
OuterLoop:
|
||||||
|
for k := range currentLineNums {
|
||||||
|
for j := range previousLineIndex {
|
||||||
|
if previousLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
previousLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
totalSum += numbers[i][k]
|
||||||
|
continue OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range currentLineIndex {
|
||||||
|
if currentLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
currentLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
totalSum += numbers[i][k]
|
||||||
|
continue OuterLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range nextLineIndex {
|
||||||
|
if nextLineIndex[j][0] >= currentLineNums[k][0] - 1 &&
|
||||||
|
nextLineIndex[j][0] <= currentLineNums[k][1] {
|
||||||
|
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 {
|
||||||
|
for j := range lastLineSymbolsIndex {
|
||||||
|
if lastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1 &&
|
||||||
|
(lastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) {
|
||||||
|
totalSum += numbers[len(lines) - 1][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for j := range notLastLineSymbolsIndex {
|
||||||
|
if (notLastLineSymbolsIndex[j][0] >= lastLineNums[i][0] - 1) &&
|
||||||
|
(notLastLineSymbolsIndex[j][0] <= lastLineNums[i][1]) {
|
||||||
|
totalSum += numbers[len(lines) - 1][i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("The total sum is: %d\n", totalSum)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user