2023-01
parent
4130343ad6
commit
9829e69813
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"git.rpjosh.de/RPJosh/go-logger"
|
||||||
"rpjosh.de/adventOfCode/internal/2023/day_01"
|
"rpjosh.de/adventOfCode/internal/2023/day_01"
|
||||||
"rpjosh.de/adventOfCode/internal/2023/day_02"
|
"rpjosh.de/adventOfCode/internal/2023/day_02"
|
||||||
"rpjosh.de/adventOfCode/internal/2023/day_03"
|
"rpjosh.de/adventOfCode/internal/2023/day_03"
|
||||||
|
@ -34,6 +35,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// Configure logger
|
||||||
|
logger.SetGlobalLogger(&logger.Logger{
|
||||||
|
Level: logger.LevelDebug,
|
||||||
|
File: &logger.FileLogger{},
|
||||||
|
})
|
||||||
|
|
||||||
year := 2023
|
year := 2023
|
||||||
day := 1
|
day := 1
|
||||||
if len(os.Args) >= 2 && os.Args[1] != "" {
|
if len(os.Args) >= 2 && os.Args[1] != "" {
|
||||||
|
|
5
go.mod
5
go.mod
|
@ -1,3 +1,8 @@
|
||||||
module rpjosh.de/adventOfCode
|
module rpjosh.de/adventOfCode
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
|
require (
|
||||||
|
git.rpjosh.de/RPJosh/go-logger v1.3.2 // indirect
|
||||||
|
golang.org/x/sys v0.15.0 // indirect
|
||||||
|
)
|
||||||
|
|
|
@ -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=
|
|
@ -1,11 +1,80 @@
|
||||||
package day_01
|
package day_01
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.rpjosh.de/RPJosh/go-logger"
|
||||||
|
"rpjosh.de/adventOfCode/pkg/utils"
|
||||||
|
)
|
||||||
|
|
||||||
type Day struct{}
|
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 {
|
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 {
|
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))
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.rpjosh.de/RPJosh/go-logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Copies the given string to the clipboard.
|
// Copies the given string to the clipboard.
|
||||||
|
@ -87,7 +89,7 @@ func PrintError(format string, a ...any) {
|
||||||
func ToInt(val string) int {
|
func ToInt(val string) int {
|
||||||
value, err := strconv.Atoi(val)
|
value, err := strconv.Atoi(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
PrintError("%s", err)
|
logger.Fatal("Failed to convert %q to a number: %s", val, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
Loading…
Reference in New Issue