From 12b3a6b293cf8afa4dfab515bac80bdf98cee3be Mon Sep 17 00:00:00 2001 From: Shaun Burdick Date: Tue, 3 Dec 2024 20:18:10 -0500 Subject: [PATCH] Increased the efficiency of day-0 as a test --- day-0/main.go | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/day-0/main.go b/day-0/main.go index dfe261c..e344efe 100644 --- a/day-0/main.go +++ b/day-0/main.go @@ -62,23 +62,23 @@ func part1(input string) int { return total } +var prefixes map[string]int = map[string]int{ + "one": 1, + "two": 2, + "three": 3, + "four": 4, + "five": 5, + "six": 6, + "seven": 7, + "eight": 8, + "nine": 9, + "zero": 0, +} + func part2(input string) int { parsed := parseInput(input) total := 0 - prefixes := map[string]int{ - "one": 1, - "two": 2, - "three": 3, - "four": 4, - "five": 5, - "six": 6, - "seven": 7, - "eight": 8, - "nine": 9, - "zero": 0, - } - for _, line := range parsed { first := -1 last := -1 @@ -93,15 +93,23 @@ func part2(input string) int { } last = num } else { - for prefix, val := range prefixes { - if checkPrefix(line[i:], prefix) { + // the number names range from 3 to 5 in length so we just need + // to check those three lengths in the map + for j := 3; j < 6; j++ { + end := i + j + // If we are over the end of the string, no point in checking + if end > len(line) { + break + } + val, ok := prefixes[line[i:end]] + if ok { if first == -1 { first = val } last = val // jump forward to the last letter of the prefix - i += len(prefix) - 2 + i += j - 2 break } } @@ -117,10 +125,3 @@ func part2(input string) int { func parseInput(input string) (ans []string) { return strings.Split(input, "\n") } - -func checkPrefix(str string, prefix string) bool { - if len(str) < len(prefix) { - return false - } - return str[:len(prefix)] == prefix -}