package dayone import ( "bufio" "fmt" "log" "os" "strings" "unicode" ) func Run() { file, err := os.Open("./day1/input") if err != nil { log.Print("error opening file") return } defer file.Close() scanner := bufio.NewScanner(file) result := 0 for scanner.Scan() { line := scanner.Text() allDigits := getDigits(line) first := allDigits[0] last := allDigits[len(allDigits) - 1] num := first * 10 + last result += num } fmt.Printf(">> day 1 result is %d", result) } func getDigits(line string) []int { result := make([]int, 0) for ind, ch := range line { if unicode.IsDigit(ch) { result = append(result, int(ch - '0')) } wordDigit, found := getTheDigitSubstringAt(line, ind) if found { result = append(result, wordDigit) } } return result } var theDigits map[string]int = map[string]int{ "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, } func getTheDigitSubstringAt(line string, index int) (digit int, found bool) { lineFromIndex := line[index:] for key, num := range theDigits { if strings.HasPrefix(lineFromIndex, key) { return num, true } } return 0, false } func getFirstDigit(line string) int { for _, ch := range line { if unicode.IsDigit(ch) { return int(ch - '0') } } return 0 } func getLastDigit(line string) int { for i := len(line) - 1; i >= 0; i-- { simbols := []rune(line) ch := simbols[i] if unicode.IsDigit(ch) { return int(ch - '0') } } return 0 }