Day 1b
This commit is contained in:
parent
255c191dfc
commit
399e40a6f9
3 changed files with 49 additions and 5 deletions
|
|
@ -8,11 +8,47 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
const digits = "0123456789"
|
||||
|
||||
func words() [10]string {
|
||||
return [10]string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
|
||||
}
|
||||
|
||||
func firstDigit(input string) string {
|
||||
var result string
|
||||
loc := len(input)
|
||||
if index := strings.IndexAny(input, "0123456789"); index != -1 {
|
||||
loc = index
|
||||
result = string([]rune(input)[index])
|
||||
}
|
||||
for key, value := range words() {
|
||||
if index := strings.Index(input, value); index != -1 && index <= loc {
|
||||
loc = index
|
||||
result = strconv.Itoa(key)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func lastDigit(input string) string {
|
||||
var result string
|
||||
loc := -1
|
||||
if index := strings.LastIndexAny(input, "0123456789"); index != -1 {
|
||||
loc = index
|
||||
result = string([]rune(input)[index])
|
||||
}
|
||||
for key, value := range words() {
|
||||
if index := strings.LastIndex(input, value); index != -1 && index >= loc {
|
||||
loc = index
|
||||
result = strconv.Itoa(key)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func getCalibrationValue(input string) int {
|
||||
firstIdx := strings.IndexAny(input, "0123456789")
|
||||
lastIdx := strings.LastIndexAny(input, "0123456789")
|
||||
first := string([]rune(input)[firstIdx])
|
||||
last := string([]rune(input)[lastIdx])
|
||||
first := firstDigit(input)
|
||||
last := lastDigit(input)
|
||||
output, _ := strconv.Atoi(first + last)
|
||||
return output
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue