advent-of-code/2023/day-1/part_one.go
Dan Anglin 76583b3dda
All checks were successful
/ test (pull_request) Successful in 25s
refactor: add common functions to internal package
It's time to add common functions to the internal 'common' package.

- Add the function that read the contents from a file to a list.
- Add the function that parses a list of integers from a string.

...and more to come later...
2023-12-05 19:43:52 +00:00

27 lines
443 B
Go

package main
func partOneCalculateSumCalibrationValues(document []string) int {
sum := 0
for _, line := range document {
sum = sum + calibrationValue(line)
}
return sum
}
func calibrationValue(line string) int {
var digits []int
for _, r := range []rune(line) {
if (r >= '0') && (r <= '9') {
digits = append(digits, int(r-'0'))
}
}
if len(digits) == 0 {
return 0
}
return (10 * digits[0]) + digits[len(digits)-1]
}