50 lines
728 B
Go
50 lines
728 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
func main() {
|
|
content, err := os.ReadFile("./input.txt")
|
|
file_content := string(content)
|
|
|
|
result := 0
|
|
|
|
if err != nil {
|
|
log.Fatalf("failed to open file: %s\n", err)
|
|
}
|
|
|
|
r := regexp.MustCompile(`mul\((\d+),(\d+)\)|do\(\)|don't\(\)`)
|
|
matches := r.FindAllStringSubmatch(file_content, -1)
|
|
|
|
do := true
|
|
|
|
for _, m := range matches {
|
|
if m[0] == "do()" {
|
|
do = true
|
|
continue
|
|
}
|
|
if m[0] == "don't()" {
|
|
do = false
|
|
continue
|
|
}
|
|
if do {
|
|
i1, err := strconv.Atoi(m[1])
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
i2, err := strconv.Atoi(m[2])
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
result += (i1 * i2)
|
|
}
|
|
}
|
|
|
|
fmt.Println("Result: ", result)
|
|
}
|