small refactor
This commit is contained in:
21
02/main.go
21
02/main.go
@@ -13,7 +13,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
inputFile = flag.String("input", "input.txt", "input filename")
|
inputFile = flag.String("input", "input.txt", "input filename")
|
||||||
subsetRegex = regexp.MustCompile(`(?mi)(\d+)+\s(\w+)([,;]?)`)
|
subsetRegex = regexp.MustCompile(`(?mi)(\d+)+\s(\w+)([,;]?)`)
|
||||||
startRegex = regexp.MustCompile(`(?mi)Game\s(\d+):`)
|
idRegex = regexp.MustCompile(`(?mi)Game\s(\d+):`)
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -32,14 +32,15 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var sum, powSum int
|
var sum int
|
||||||
|
var powSum float64
|
||||||
for _, line := range strings.Split(string(input), "\n") {
|
for _, line := range strings.Split(string(input), "\n") {
|
||||||
if strings.TrimSpace(line) == "" {
|
if strings.TrimSpace(line) == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// find id
|
// find id
|
||||||
idMatch := startRegex.FindAllStringSubmatch(line, -1)
|
idMatch := idRegex.FindAllStringSubmatch(line, -1)
|
||||||
id, err := strconv.Atoi(idMatch[0][1])
|
id, err := strconv.Atoi(idMatch[0][1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -48,12 +49,12 @@ func main() {
|
|||||||
// find sets of digit + color + separator (e.g. "3 blue,")
|
// find sets of digit + color + separator (e.g. "3 blue,")
|
||||||
subsets := subsetRegex.FindAllStringSubmatch(line, -1)
|
subsets := subsetRegex.FindAllStringSubmatch(line, -1)
|
||||||
|
|
||||||
var red, green, blue int
|
var red, green, blue float64
|
||||||
var minRed, minGreen, minBlue int
|
var minRed, minGreen, minBlue float64
|
||||||
possible := true
|
possible := true
|
||||||
|
|
||||||
for _, subset := range subsets {
|
for _, subset := range subsets {
|
||||||
cubeAmount, err := strconv.Atoi(subset[1])
|
cubeAmount, err := strconv.ParseFloat(subset[1], 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -73,9 +74,9 @@ func main() {
|
|||||||
if red > MaxRed || green > MaxGreen || blue > MaxBlue {
|
if red > MaxRed || green > MaxGreen || blue > MaxBlue {
|
||||||
possible = false
|
possible = false
|
||||||
}
|
}
|
||||||
minBlue = int(math.Max(float64(minBlue), float64(blue)))
|
minBlue = math.Max(minBlue, blue)
|
||||||
minRed = int(math.Max(float64(minRed), float64(red)))
|
minRed = math.Max(minRed, red)
|
||||||
minGreen = int(math.Max(float64(minGreen), float64(green)))
|
minGreen = math.Max(minGreen, green)
|
||||||
red = 0
|
red = 0
|
||||||
blue = 0
|
blue = 0
|
||||||
green = 0
|
green = 0
|
||||||
@@ -91,5 +92,5 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Sum of IDs: %d\n", sum)
|
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))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user