-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.go
More file actions
72 lines (58 loc) · 1.51 KB
/
day03.go
File metadata and controls
72 lines (58 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"AdventOfCode-go/advent2024/utils"
"fmt"
"regexp"
)
func day03(filename string, part byte, debug bool) int {
var result int
puzzleInput, _ := utils.ReadFile(filename)
if part == 'a' {
for _, puzzleLine := range puzzleInput {
reg := regexp.MustCompile(`mul\(\d+,\d+\)`)
regMatched := reg.FindAllStringSubmatch(puzzleLine, -1)
for _, item := range regMatched {
var firstNum, secondNum int
fmt.Sscanf(item[0], "mul(%d,%d)", &firstNum, &secondNum)
result += firstNum * secondNum
}
}
return result
}
// Part 2: we need to take notice of the do() and don't() commands in the instruction list
var useStatement bool = true
for _, puzzleLine := range puzzleInput {
reg := regexp.MustCompile(`mul\(\d+,\d+\)|do\(\)|don't\(\)`)
regMatched := reg.FindAllStringSubmatch(puzzleLine, -1)
if debug {
fmt.Println(regMatched)
}
for _, item := range regMatched {
if debug {
fmt.Println(item)
}
switch item[0] {
case "do()":
useStatement = true
case "don't()":
useStatement = false
default:
if useStatement {
var firstNum, secondNum int
fmt.Sscanf(item[0], "mul(%d,%d)", &firstNum, &secondNum)
result += firstNum * secondNum
}
}
}
}
return result
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", day03(filenamePtr, execPart, debug))
}
}