This commit is contained in:
2023-12-02 16:36:49 +01:00
parent 914adc63e6
commit db32ffa315
7 changed files with 173 additions and 61 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"math"
"os"
"regexp"
"strconv"
@@ -10,19 +11,18 @@ import (
)
var (
inputFile = flag.String("input", "input.txt", "input filename")
nRegex = regexp.MustCompile(`(?m)\d`)
reMap = map[string]string{
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
inputFile = flag.String("input", "input.txt", "input filename")
subsetRegex = regexp.MustCompile(`(?mi)(\d+)+\s(\w+)([,;]?)`)
startRegex = regexp.MustCompile(`(?mi)Game\s(\d+):`)
)
const (
BlueCube = "blue"
GreenCube = "green"
RedCube = "red"
MaxRed = 12
MaxGreen = 13
MaxBlue = 14
)
func main() {
@@ -32,61 +32,64 @@ func main() {
panic(err)
}
var sum int
var sum, powSum int
for _, line := range strings.Split(string(input), "\n") {
if strings.TrimSpace(line) == "" {
continue
}
// forward-scan for spelled digits
var forwardReplacedLine, backwardReplacedLine string
for i := 0; i < len(line); i++ {
forwardReplacedLine += string(line[i])
// replace all potential spelled out digits by their non-word counterpart (e.g. "one" -> "1")
for key, val := range reMap {
forwardReplacedLine = strings.ReplaceAll(forwardReplacedLine, key, val)
}
}
fmt.Println(forwardReplacedLine)
// backward-scan for spelled digits
for i := len(line) - 1; i >= 0; i-- {
if i == len(line)-1 {
backwardReplacedLine = string(line[i])
} else {
backwardReplacedLine = string(line[i]) + backwardReplacedLine
}
// replace all potential spelled out digits by their non-word counterpart (e.g. "one" -> "1")
for key, val := range reMap {
backwardReplacedLine = strings.ReplaceAll(backwardReplacedLine, key, val)
}
}
fmt.Println(backwardReplacedLine)
// find single digits in forward-replaced line
firstMatch := nRegex.FindAllStringSubmatch(forwardReplacedLine, -1)
// find single digits in backward-replaced line
lastMatch := nRegex.FindAllStringSubmatch(backwardReplacedLine, -1)
// get first matchgroup, first match (aka first digit in forwardReplacedLine)
first := firstMatch[0][0]
// get last matchgroup, first match (aka last digit in backwardReplacedLine)
last := lastMatch[len(lastMatch)-1:][0][0]
fmt.Println(first + last)
// concatenate digits and convert the resulting string to int
asInt, err := strconv.Atoi(first + last)
// find id
idMatch := startRegex.FindAllStringSubmatch(line, -1)
id, err := strconv.Atoi(idMatch[0][1])
if err != nil {
panic(err)
}
sum += asInt
// find sets of digit + color + separator (e.g. "3 blue,")
subsets := subsetRegex.FindAllStringSubmatch(line, -1)
var red, green, blue int
var minRed, minGreen, minBlue int
possible := true
for _, subset := range subsets {
cubeAmount, err := strconv.Atoi(subset[1])
if err != nil {
panic(err)
}
// add cube amount to set
switch strings.ToLower(subset[2]) {
case BlueCube:
blue += cubeAmount
case GreenCube:
green += cubeAmount
case RedCube:
red += cubeAmount
}
// end of set reached
if subset[3] == "" || subset[3] == ";" {
if red > MaxRed || green > MaxGreen || blue > MaxBlue {
possible = false
}
minBlue = int(math.Max(float64(minBlue), float64(blue)))
minRed = int(math.Max(float64(minRed), float64(red)))
minGreen = int(math.Max(float64(minGreen), float64(green)))
red = 0
blue = 0
green = 0
}
}
// end of game reached
powSum += minRed * minGreen * minBlue
if possible {
fmt.Printf("%d possible\n", id)
sum += id
}
}
fmt.Println(sum)
fmt.Printf("Sum of IDs: %d\n", sum)
fmt.Printf("Sum of the power of sets: %d", powSum)
}