Solve 2024 day 3 in go

This commit is contained in:
SebastianStork 2025-01-08 21:09:57 +01:00
parent af46c2fd1f
commit cd5994680d
3 changed files with 67 additions and 0 deletions

58
2024/go/day-03/main.go Normal file
View file

@ -0,0 +1,58 @@
package main
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
)
func readInput() (string, error) {
content, err := os.ReadFile("input")
if err != nil {
return "", err
}
return string(content), nil
}
func instructionsResult(memory string) (int, int) {
regex := regexp.MustCompile(`mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)`)
matches := regex.FindAllStringSubmatch(memory, -1)
var totalSum, conditionalSum int
do := true
for _, match := range matches {
switch match[0] {
case "do()":
do = true
case "don't()":
do = false
default:
num1, _ := strconv.Atoi(match[1])
num2, _ := strconv.Atoi(match[2])
totalSum += num1 * num2
if do {
conditionalSum += num1 * num2
}
}
}
return totalSum, conditionalSum
}
func main() {
memory, err := readInput()
if err != nil {
log.Fatalln(err)
}
totalSum, conditionalSum := instructionsResult(memory)
// Part one
fmt.Println("Sum of the instructions:", totalSum)
// Part two
fmt.Println("Sum of the instructions but with conditionals:", conditionalSum)
}