This repository was archived by the owner on Dec 28, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ information.
21
21
| -----| ----------| -----| ----------|
22
22
| 01 | ⭐ ⭐ | 14 | |
23
23
| 02 | ⭐ ⭐ | 15 | |
24
- | 03 | | 16 | |
24
+ | 03 | ⭐ | 16 | |
25
25
| 04 | | 17 | |
26
26
| 05 | | 18 | |
27
27
| 06 | | 19 | |
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
+ "fmt"
4
5
"github.com/terminalnode/adventofcode2024/common"
6
+ "regexp"
7
+ "strconv"
5
8
)
6
9
10
+ var findMulRegex = regexp .MustCompile ("mul\\ ((\\ d+),(\\ d+)\\ )" )
11
+
7
12
func main () {
8
13
common .Setup (2 , part1 , part2 )
9
14
}
10
15
11
16
func part1 (
12
17
input string ,
13
18
) 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 )
15
33
}
16
34
17
35
func part2 (
18
36
input string ,
19
37
) string {
20
38
return "Not solved yet"
21
39
}
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
+ }
You can’t perform that action at this time.
0 commit comments