|
|
|
@ -17,62 +17,61 @@ const (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func partTwoCalculateSumCalibrationValues(document []string) int {
|
|
|
|
|
var (
|
|
|
|
|
digits []int
|
|
|
|
|
sum int
|
|
|
|
|
)
|
|
|
|
|
sum := 0
|
|
|
|
|
|
|
|
|
|
for _, line := range document {
|
|
|
|
|
sum = sum + partTwoCalibrationValue(line, digits)
|
|
|
|
|
sum = sum + partTwoCalibrationValue(line)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sum
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func partTwoCalibrationValue(line string, digits []int) int {
|
|
|
|
|
if len(line) > 0 {
|
|
|
|
|
func partTwoCalibrationValue(line string) int {
|
|
|
|
|
var digits []int
|
|
|
|
|
|
|
|
|
|
for len(line) > 0 {
|
|
|
|
|
switch {
|
|
|
|
|
case (line[0] >= '0') && (line[0] <= '9'):
|
|
|
|
|
digits = append(digits, int(line[0]-'0'))
|
|
|
|
|
return partTwoCalibrationValue(line[1:], digits)
|
|
|
|
|
line = line[1:]
|
|
|
|
|
case strings.HasPrefix(line, one):
|
|
|
|
|
digits = append(digits, 1)
|
|
|
|
|
trimLength := len(one) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
case strings.HasPrefix(line, two):
|
|
|
|
|
digits = append(digits, 2)
|
|
|
|
|
trimLength := len(two) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
case strings.HasPrefix(line, three):
|
|
|
|
|
digits = append(digits, 3)
|
|
|
|
|
trimLength := len(three) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
case strings.HasPrefix(line, four):
|
|
|
|
|
digits = append(digits, 4)
|
|
|
|
|
trimLength := len(four) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
case strings.HasPrefix(line, five):
|
|
|
|
|
digits = append(digits, 5)
|
|
|
|
|
trimLength := len(five) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
case strings.HasPrefix(line, six):
|
|
|
|
|
digits = append(digits, 6)
|
|
|
|
|
trimLength := len(six) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
case strings.HasPrefix(line, seven):
|
|
|
|
|
digits = append(digits, 7)
|
|
|
|
|
trimLength := len(seven) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
case strings.HasPrefix(line, eight):
|
|
|
|
|
digits = append(digits, 8)
|
|
|
|
|
trimLength := len(eight) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
case strings.HasPrefix(line, nine):
|
|
|
|
|
digits = append(digits, 9)
|
|
|
|
|
trimLength := len(nine) - 1
|
|
|
|
|
return partTwoCalibrationValue(line[trimLength:], digits)
|
|
|
|
|
line = line[trimLength:]
|
|
|
|
|
default:
|
|
|
|
|
return partTwoCalibrationValue(line[1:], digits)
|
|
|
|
|
line = line[1:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|