Skip to content

Commit 35e7599

Browse files
committed
[2024] Try using regexp for Day 1
1 parent b084ddc commit 35e7599

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

2024/day01/main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"fmt"
55
"log"
66
"os"
7-
"strings"
7+
"regexp"
88

99
"github.com/kfarnung/advent-of-code/2024/lib"
1010
)
1111

12+
var lineParseRegex = regexp.MustCompile(`^(\d+) +(\d+)$`)
13+
1214
func part1(input string) int64 {
1315
first, second, err := parseInput(input)
1416
if err != nil {
@@ -50,19 +52,19 @@ func part2(input string) int64 {
5052
func parseInput(input string) ([]int64, []int64, error) {
5153
var first []int64
5254
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 {
5659
continue
5760
}
5861

59-
splitLine := strings.Split(line, " ")
60-
firstValue, err := lib.ParseInt64(splitLine[0])
62+
firstValue, err := lib.ParseInt64(matches[1])
6163
if err != nil {
6264
return nil, nil, err
6365
}
6466

65-
secondValue, err := lib.ParseInt64(splitLine[len(splitLine)-1])
67+
secondValue, err := lib.ParseInt64(matches[2])
6668
if err != nil {
6769
return nil, nil, err
6870
}

2024/day01/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
var input string = strings.Join([]string{
11+
var input = strings.Join([]string{
1212
"3 4",
1313
"4 3",
1414
"2 5",

0 commit comments

Comments
 (0)