mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-24 19:30:48 +00:00
28 lines
558 B
Go
28 lines
558 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
// one takes a string array and for each line:
|
|
// Combine the first digit and the last digit (in that order) to form a single two-digit number.
|
|
// Returns the sum of all calibrated values.
|
|
func one(lines []string) (int, error) {
|
|
re := regexp.MustCompile(`\d`)
|
|
|
|
sum := 0
|
|
for _, line := range lines {
|
|
m := re.FindAllString(line, -1)
|
|
if len(m) > 0 {
|
|
num, err := strconv.Atoi(fmt.Sprintf("%s%s", m[0], m[len(m)-1]))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
sum += num
|
|
}
|
|
}
|
|
return sum, nil
|
|
}
|