aoc2023/day-2/two.go

50 lines
922 B
Go
Raw Normal View History

2023-12-02 20:13:42 +00:00
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 {
2023-12-02 20:13:42 +00:00
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
}