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 (
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+
1214func part1 (input string ) int64 {
1315 first , second , err := parseInput (input )
1416 if err != nil {
@@ -50,19 +52,19 @@ func part2(input string) int64 {
5052func 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 }
Original file line number Diff line number Diff line change 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" ,
You can’t perform that action at this time.
0 commit comments