Skip to content

Commit

Permalink
[2024] Try using regexp for Day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarnung committed Dec 2, 2024
1 parent b084ddc commit 35e7599
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions 2024/day01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"fmt"
"log"
"os"
"strings"
"regexp"

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

var lineParseRegex = regexp.MustCompile(`^(\d+) +(\d+)$`)

func part1(input string) int64 {
first, second, err := parseInput(input)
if err != nil {
Expand Down Expand Up @@ -50,19 +52,19 @@ func part2(input string) int64 {
func parseInput(input string) ([]int64, []int64, error) {
var first []int64
var second []int64
lines := lib.SplitLines(input)
for _, line := range lines {
if (len(line)) == 0 {

for _, line := range lib.SplitLines(input) {
matches := lineParseRegex.FindStringSubmatch(line)
if matches == nil {
continue
}

splitLine := strings.Split(line, " ")
firstValue, err := lib.ParseInt64(splitLine[0])
firstValue, err := lib.ParseInt64(matches[1])
if err != nil {
return nil, nil, err
}

secondValue, err := lib.ParseInt64(splitLine[len(splitLine)-1])
secondValue, err := lib.ParseInt64(matches[2])
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion 2024/day01/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

var input string = strings.Join([]string{
var input = strings.Join([]string{
"3 4",
"4 3",
"2 5",
Expand Down

0 comments on commit 35e7599

Please sign in to comment.