add 2024/day3
This commit is contained in:
parent
02b95a7e65
commit
a7c0239f66
4 changed files with 90 additions and 0 deletions
52
2024/03/main.go
Normal file
52
2024/03/main.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"aoc/helper"
|
||||
)
|
||||
|
||||
func main() {
|
||||
corruptedMemory := helper.ReadLinesToString()
|
||||
|
||||
// part 1
|
||||
instructions_result := findAndExecuteInstructions(corruptedMemory)
|
||||
fmt.Printf("Result: %d\n", instructions_result)
|
||||
|
||||
// part 2
|
||||
enabled_instructions_result := findAndExecuteEnabledInstructions(corruptedMemory)
|
||||
fmt.Printf("Result (only enabled): %d\n", enabled_instructions_result)
|
||||
}
|
||||
|
||||
func findAndExecuteInstructions(memory string) (res int) {
|
||||
re := regexp.MustCompile(`mul\((\d{1,3}),(\d{1,3})\)`)
|
||||
instructions := re.FindAllStringSubmatch(memory, -1)
|
||||
|
||||
for _, inst := range instructions {
|
||||
res += helper.ToInt(inst[1]) * helper.ToInt(inst[2])
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func findAndExecuteEnabledInstructions(memory string) (res int) {
|
||||
re := regexp.MustCompile(`mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)`)
|
||||
instructions := re.FindAllStringSubmatch(memory, -1)
|
||||
|
||||
enabled := true
|
||||
for _, inst := range instructions {
|
||||
switch inst[0] {
|
||||
case "do()":
|
||||
enabled = true
|
||||
case "don't()":
|
||||
enabled = false
|
||||
default:
|
||||
if enabled {
|
||||
res += helper.ToInt(inst[1]) * helper.ToInt(inst[2])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue