mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-22 02:10:48 +00:00
50 lines
922 B
Go
50 lines
922 B
Go
package main
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// powerForGame returns the power of the minimum set of cubes for a game
|
|
func powerForGame(data string) (int, error) {
|
|
var counter = map[string]int{
|
|
"red": 0,
|
|
"green": 0,
|
|
"blue": 0,
|
|
}
|
|
|
|
for _, colour := range []string{"red", "green", "blue"} {
|
|
m := regex[colour].FindAllStringSubmatch(data, -1)
|
|
for _, s := range m {
|
|
n, err := strconv.Atoi(s[1])
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if counter[colour] < n {
|
|
counter[colour] = n
|
|
}
|
|
}
|
|
}
|
|
return counter["red"] * counter["green"] * counter["blue"], nil
|
|
}
|
|
|
|
// two returns the sum of powers for all games
|
|
func two(lines []string) (int, error) {
|
|
sum := 0
|
|
|
|
for _, line := range lines {
|
|
_, data := func() (string, string) {
|
|
x := strings.Split(line, ":")
|
|
return x[0], x[1]
|
|
}()
|
|
|
|
pow, err := powerForGame(data)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
sum += pow
|
|
}
|
|
|
|
return sum, nil
|
|
}
|