Last active
December 2, 2023 01:04
-
-
Save zyxkad/e30999e17636f2526b1f0143ae5e206d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "strings" | |
| "time" | |
| ) | |
| type Pair struct { | |
| s string | |
| n int | |
| } | |
| const ( | |
| minLen = 3 | |
| maxLen = 5 | |
| ) | |
| var pairs = [][]Pair{ | |
| nil, | |
| nil, | |
| nil, | |
| { | |
| {"one", 1}, | |
| {"six", 6}, | |
| {"two", 2}, | |
| }, | |
| { | |
| {"five", 5}, | |
| {"four", 4}, | |
| {"nine", 9}, | |
| }, | |
| { | |
| {"eight", 8}, | |
| {"seven", 7}, | |
| {"three", 3}, | |
| }, | |
| } | |
| var pairs10 = [][]Pair{ | |
| nil, | |
| nil, | |
| nil, | |
| { | |
| {"one", 10}, | |
| {"six", 60}, | |
| {"two", 20}, | |
| }, | |
| { | |
| {"five", 50}, | |
| {"four", 40}, | |
| {"nine", 90}, | |
| }, | |
| { | |
| {"eight", 80}, | |
| {"seven", 70}, | |
| {"three", 30}, | |
| }, | |
| } | |
| func main() { | |
| start := time.Now() | |
| sc := bufio.NewScanner(strings.NewReader(input)) | |
| n := 0 | |
| for sc.Scan() { | |
| x := scanDigit(sc.Text()) | |
| n += x | |
| } | |
| used := time.Since(start) | |
| fmt.Println(n) | |
| fmt.Println("used:", used.String()) | |
| } | |
| var digits = [256]int{ | |
| '1': 1, | |
| '2': 2, | |
| '3': 3, | |
| '4': 4, | |
| '5': 5, | |
| '6': 6, | |
| '7': 7, | |
| '8': 8, | |
| '9': 9, | |
| } | |
| var digits10 = [256]int{ | |
| '1': 10, | |
| '2': 20, | |
| '3': 30, | |
| '4': 40, | |
| '5': 50, | |
| '6': 60, | |
| '7': 70, | |
| '8': 80, | |
| '9': 90, | |
| } | |
| func scanDigit(s string) (n int) { | |
| leng := len(s) | |
| i := 0 | |
| for i <= minLen { | |
| c := s[i] | |
| if x := digits10[c]; x != 0 { | |
| n = x | |
| goto E | |
| } | |
| i++ | |
| for j := 0; j + minLen <= i; j++ { | |
| ss := s[j:i] | |
| for _, p := range pairs10[len(ss)] { | |
| if ss == p.s { | |
| n = p.n | |
| goto E | |
| } | |
| } | |
| } | |
| } | |
| for i < leng { | |
| c := s[i] | |
| if x := digits10[c]; x != 0 { | |
| n = x | |
| goto E | |
| } | |
| i++ | |
| for j := i - maxLen; j + minLen <= i; j++ { | |
| ss := s[j:i] | |
| for _, p := range pairs10[len(ss)] { | |
| if ss == p.s { | |
| n = p.n | |
| goto E | |
| } | |
| } | |
| } | |
| } | |
| E: | |
| r := leng - maxLen | |
| i = leng | |
| for i > r { | |
| i-- | |
| c := s[i] | |
| if x := digits[c]; x != 0 { | |
| n += x | |
| goto F | |
| } | |
| for j := i + minLen; j <= leng; j++ { | |
| ss := s[i:j] | |
| for _, p := range pairs[len(ss)] { | |
| if ss == p.s { | |
| n += p.n | |
| goto F | |
| } | |
| } | |
| } | |
| } | |
| for i >= 0 { | |
| i-- | |
| c := s[i] | |
| if x := digits[c]; x != 0 { | |
| n += x | |
| goto F | |
| } | |
| for j := minLen; j <= maxLen; j++ { | |
| ss := s[i:i + j] | |
| for _, p := range pairs[len(ss)] { | |
| if ss == p.s { | |
| n += p.n | |
| goto F | |
| } | |
| } | |
| } | |
| } | |
| F: | |
| return | |
| } | |
| const input = `two1nine | |
| eightwothree | |
| abcone2threexyz | |
| xtwone3four | |
| 4nineeightseven2 | |
| zoneight234 | |
| 7pqrstsixteen` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment