Skip to content

Commit d120fa2

Browse files
committed
day2: solved part 2
1 parent 3d25aa0 commit d120fa2

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed
File renamed without changes.

day3/example2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
2+

day3/main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ func main() {
1717
text += scanner.Text()
1818
}
1919

20-
pattern := regexp.MustCompile(`mul\((\d+),(\d+)\)`)
20+
pattern := regexp.MustCompile(`(do|don't)\(\)|mul\((\d+),(\d+)\)`)
2121
result := 0
22+
do := true
2223
for _, match := range pattern.FindAllStringSubmatch(text, -1) {
23-
n1 := util.Atoi(match[1])
24-
n2 := util.Atoi(match[2])
25-
result += n1 * n2
24+
fmt.Printf("%v\n", match)
25+
if match[1] == "do" {
26+
do = true
27+
} else if match[1] == "don't" {
28+
do = false
29+
} else if do {
30+
n1 := util.Atoi(match[2])
31+
n2 := util.Atoi(match[3])
32+
result += n1 * n2
33+
}
2634
}
2735

2836
fmt.Printf("result: %d\n", result)

0 commit comments

Comments
 (0)