small refactor

This commit is contained in:
2023-12-02 16:44:55 +01:00
parent db32ffa315
commit 316d1052a4

View File

@@ -13,7 +13,7 @@ import (
var (
inputFile = flag.String("input", "input.txt", "input filename")
subsetRegex = regexp.MustCompile(`(?mi)(\d+)+\s(\w+)([,;]?)`)
startRegex = regexp.MustCompile(`(?mi)Game\s(\d+):`)
idRegex = regexp.MustCompile(`(?mi)Game\s(\d+):`)
)
const (
@@ -32,14 +32,15 @@ func main() {
panic(err)
}
var sum, powSum int
var sum int
var powSum float64
for _, line := range strings.Split(string(input), "\n") {
if strings.TrimSpace(line) == "" {
continue
}
// find id
idMatch := startRegex.FindAllStringSubmatch(line, -1)
idMatch := idRegex.FindAllStringSubmatch(line, -1)
id, err := strconv.Atoi(idMatch[0][1])
if err != nil {
panic(err)
@@ -48,12 +49,12 @@ func main() {
// 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
var red, green, blue float64
var minRed, minGreen, minBlue float64
possible := true
for _, subset := range subsets {
cubeAmount, err := strconv.Atoi(subset[1])
cubeAmount, err := strconv.ParseFloat(subset[1], 64)
if err != nil {
panic(err)
}
@@ -73,9 +74,9 @@ func main() {
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)))
minBlue = math.Max(minBlue, blue)
minRed = math.Max(minRed, red)
minGreen = math.Max(minGreen, green)
red = 0
blue = 0
green = 0
@@ -91,5 +92,5 @@ func main() {
}
fmt.Printf("Sum of IDs: %d\n", sum)
fmt.Printf("Sum of the power of sets: %d", powSum)
fmt.Printf("Sum of the power of sets: %d", int(powSum))
}