File tree Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Original file line number Diff line number Diff line change @@ -4,11 +4,13 @@ import (
4
4
"fmt"
5
5
"log"
6
6
"os"
7
- "strings "
7
+ "regexp "
8
8
9
9
"github.com/kfarnung/advent-of-code/2024/lib"
10
10
)
11
11
12
+ var lineParseRegex = regexp .MustCompile (`^(\d+) +(\d+)$` )
13
+
12
14
func part1 (input string ) int64 {
13
15
first , second , err := parseInput (input )
14
16
if err != nil {
@@ -50,19 +52,19 @@ func part2(input string) int64 {
50
52
func parseInput (input string ) ([]int64 , []int64 , error ) {
51
53
var first []int64
52
54
var second []int64
53
- lines := lib .SplitLines (input )
54
- for _ , line := range lines {
55
- if (len (line )) == 0 {
55
+
56
+ for _ , line := range lib .SplitLines (input ) {
57
+ matches := lineParseRegex .FindStringSubmatch (line )
58
+ if matches == nil {
56
59
continue
57
60
}
58
61
59
- splitLine := strings .Split (line , " " )
60
- firstValue , err := lib .ParseInt64 (splitLine [0 ])
62
+ firstValue , err := lib .ParseInt64 (matches [1 ])
61
63
if err != nil {
62
64
return nil , nil , err
63
65
}
64
66
65
- secondValue , err := lib .ParseInt64 (splitLine [ len ( splitLine ) - 1 ])
67
+ secondValue , err := lib .ParseInt64 (matches [ 2 ])
66
68
if err != nil {
67
69
return nil , nil , err
68
70
}
Original file line number Diff line number Diff line change 8
8
"github.com/stretchr/testify/assert"
9
9
)
10
10
11
- var input string = strings .Join ([]string {
11
+ var input = strings .Join ([]string {
12
12
"3 4" ,
13
13
"4 3" ,
14
14
"2 5" ,
You can’t perform that action at this time.
0 commit comments