64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
// "strings"
|
|
)
|
|
|
|
func main() {
|
|
filePath := "input"
|
|
file, err := os.Open(filePath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
result := 0
|
|
defer file.Close()
|
|
scanner := bufio.NewScanner(file)
|
|
r := regexp.MustCompile(`mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don't\(\)`)
|
|
numbers := regexp.MustCompile(`[0-9]{1,3}`)
|
|
enabled := true
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
matches := r.FindAllString(line, -1)
|
|
fmt.Println(matches)
|
|
for i := range matches {
|
|
if matches[i] == "do()" {
|
|
fmt.Println("Do")
|
|
enabled = true
|
|
} else if matches[i] == "don't()" {
|
|
fmt.Println("Don't")
|
|
enabled = false
|
|
} else {
|
|
if enabled {
|
|
fmt.Println(matches[i], " enabled")
|
|
multipliers := numbers.FindAllString(matches[i], -1)
|
|
firstNumber, _ := strconv.Atoi(multipliers[0])
|
|
secondNumber, _ := strconv.Atoi(multipliers[1])
|
|
result = result + firstNumber * secondNumber
|
|
} else {
|
|
fmt.Println(matches[i], " disabled")
|
|
}
|
|
}
|
|
// multipliers := numbers.FindAllString(matches[i], -1)
|
|
//// fmt.Println(multipliers)
|
|
//// fmt.Printf("%T\n", multipliers)
|
|
//// fmt.Println(len(multipliers))
|
|
//// fmt.Println(multipliers[0])
|
|
// firstNumber, _ := strconv.Atoi(multipliers[0])
|
|
// secondNumber, _ := strconv.Atoi(multipliers[1])
|
|
// result = result + firstNumber * secondNumber
|
|
//// for j := range multipliers {
|
|
//// multipliersSplit := strings.Fields(multipliers[j])
|
|
//// multiplierInt1, _ := strconv.Atoi(multipliersSplit[0])
|
|
//// multiplierInt2, _ := strconv.Atoi(multipliersSplit[1])
|
|
//// result = result + multiplierInt1 * multiplierInt2
|
|
//// }
|
|
}
|
|
}
|
|
fmt.Println(result)
|
|
}
|