Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 044900d

Browse files
committed
Add solution for day 3 part 1
1 parent 01e3993 commit 044900d

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ information.
2121
|-----|----------|-----|----------|
2222
| 01 | ⭐ ⭐ | 14 | |
2323
| 02 | ⭐ ⭐ | 15 | |
24-
| 03 | | 16 | |
24+
| 03 | | 16 | |
2525
| 04 | | 17 | |
2626
| 05 | | 18 | |
2727
| 06 | | 19 | |

day03/main.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/terminalnode/adventofcode2024/common"
6+
"regexp"
7+
"strconv"
58
)
69

10+
var findMulRegex = regexp.MustCompile("mul\\((\\d+),(\\d+)\\)")
11+
712
func main() {
813
common.Setup(2, part1, part2)
914
}
1015

1116
func part1(
1217
input string,
1318
) string {
14-
return "Not solved yet"
19+
finds := findMulRegex.FindAllSubmatch([]byte(input), -1)
20+
sum := 0
21+
for _, match := range finds {
22+
if len(match) != 3 {
23+
return fmt.Sprintf("Expected 3 results from regex, got %q\n", match)
24+
}
25+
26+
multiplied, err := mul(string(match[1]), string(match[2]))
27+
if err != nil {
28+
return fmt.Sprintf("Failed to parse multiplication of %q:\n%v\n", match, err)
29+
}
30+
sum += multiplied
31+
}
32+
return fmt.Sprintf("Sum of all %d multiplications: %d", len(finds), sum)
1533
}
1634

1735
func part2(
1836
input string,
1937
) string {
2038
return "Not solved yet"
2139
}
40+
41+
func mul(sub1 string, sub2 string) (int, error) {
42+
first, err := strconv.ParseInt(sub1, 10, 0)
43+
if err != nil {
44+
return 0, err
45+
}
46+
47+
second, err := strconv.ParseInt(sub2, 10, 0)
48+
if err != nil {
49+
return 0, err
50+
}
51+
52+
return int(first * second), nil
53+
}

0 commit comments

Comments
 (0)