From 9829e698138b7ec5e94dda5acf18840c80f9a360 Mon Sep 17 00:00:00 2001 From: RPJosh Date: Fri, 1 Dec 2023 13:48:20 +0100 Subject: [PATCH] 2023-01 --- cmd/adventOfCode/main.go | 8 +++++ go.mod | 5 +++ go.sum | 4 +++ internal/2023/day_01/in.go | 73 ++++++++++++++++++++++++++++++++++++-- pkg/utils/utils.go | 4 ++- 5 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 go.sum diff --git a/cmd/adventOfCode/main.go b/cmd/adventOfCode/main.go index dc24210..75aec59 100644 --- a/cmd/adventOfCode/main.go +++ b/cmd/adventOfCode/main.go @@ -5,6 +5,7 @@ import ( "os" "strconv" + "git.rpjosh.de/RPJosh/go-logger" "rpjosh.de/adventOfCode/internal/2023/day_01" "rpjosh.de/adventOfCode/internal/2023/day_02" "rpjosh.de/adventOfCode/internal/2023/day_03" @@ -34,6 +35,13 @@ import ( ) func main() { + + // Configure logger + logger.SetGlobalLogger(&logger.Logger{ + Level: logger.LevelDebug, + File: &logger.FileLogger{}, + }) + year := 2023 day := 1 if len(os.Args) >= 2 && os.Args[1] != "" { diff --git a/go.mod b/go.mod index c4d48d1..ed67570 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module rpjosh.de/adventOfCode go 1.18 + +require ( + git.rpjosh.de/RPJosh/go-logger v1.3.2 // indirect + golang.org/x/sys v0.15.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c3b8d9c --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +git.rpjosh.de/RPJosh/go-logger v1.3.2 h1:y8qFEBYeJzLLi6H7CpHHGb2pB0IyfHSG6m6o8TxL1uo= +git.rpjosh.de/RPJosh/go-logger v1.3.2/go.mod h1:iD3KaRyOIkYMj7E+xFMn5uDVCzW1lSJQopz1Fl1+BSM= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/internal/2023/day_01/in.go b/internal/2023/day_01/in.go index 190cf49..f503f30 100644 --- a/internal/2023/day_01/in.go +++ b/internal/2023/day_01/in.go @@ -1,11 +1,80 @@ package day_01 +import ( + "fmt" + "regexp" + "strings" + + "git.rpjosh.de/RPJosh/go-logger" + "rpjosh.de/adventOfCode/pkg/utils" +) + type Day struct{} +// findFirstAndLastNumber finds the first and last occurence of a number by the given regex +// expression for the number (within a match group). +// If the found string value is present in the map, the mapped value is used +func (d *Day) findFirstAndLastNumber(in string, regex string, stringMap map[string]int) int { + // Build regex to get the first and last number in the string (because of greediness we get the last number) + regexFirst := regexp.MustCompile(`^.*?` + regex + `.*`) + regexLast := regexp.MustCompile(`^.*` + regex) + count := 0 + + for _, val := range strings.Split(in, "\n") { + if val == "" { + continue + } + + matchesFirst := regexFirst.FindStringSubmatch(val) + matchesLast := regexLast.FindStringSubmatch(val) + + // We expect exactly at least one match + if len(matchesFirst) < 1 { + logger.Fatal("Failed to get at least one number (got %d): %q", len(matchesFirst), val) + } + + str := d.replaceDigitByInt(matchesFirst[1], stringMap) + if len(matchesLast) > 1 { + str += d.replaceDigitByInt(matchesLast[1], stringMap) + } + + count += utils.ToInt(str) + } + + return count +} + +func (d *Day) replaceDigitByInt(val string, stringMap map[string]int) string { + if v, exists := stringMap[val]; exists { + return fmt.Sprintf("%d", v) + } + + return val +} + func (d *Day) Part1(in string) string { - return "" + return fmt.Sprintf("%d", d.findFirstAndLastNumber(in, `(\d)`, make(map[string]int))) } func (d *Day) Part2(in string) string { - return "" + digist := map[string]int{ + "one": 1, + "two": 2, + "three": 3, + "four": 4, + "five": 5, + "six": 6, + "seven": 7, + "eight": 8, + "nine": 9, + } + + // Build regex string by looping through all digists + regex := `(\d` + for k, _ := range digist { + regex += "|" + k + } + regex += ")" + + return fmt.Sprintf("%d", d.findFirstAndLastNumber(in, regex, digist)) } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index dfe488b..91ad06a 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -9,6 +9,8 @@ import ( "strconv" "strings" "time" + + "git.rpjosh.de/RPJosh/go-logger" ) // Copies the given string to the clipboard. @@ -87,7 +89,7 @@ func PrintError(format string, a ...any) { func ToInt(val string) int { value, err := strconv.Atoi(val) if err != nil { - PrintError("%s", err) + logger.Fatal("Failed to convert %q to a number: %s", val, err) } return value