day2, part2

This commit is contained in:
efim 2023-12-02 16:29:39 +00:00
parent 0f9d7ccbe9
commit afde84519f
1 changed files with 26 additions and 4 deletions

View File

@ -40,9 +40,7 @@ func Run() int {
line := scanner.Text() line := scanner.Text()
game := ReadLine(line) game := ReadLine(line)
log.Printf("reading %s got game %+v it is %t", line, game, game.isPossible(Restriction)) log.Printf("reading %s got game %+v it is %t", line, game, game.isPossible(Restriction))
if game.isPossible(Restriction) { result += game.minKubesPower()
result += game.Num
}
} }
return result return result
@ -60,6 +58,30 @@ func (g Game)isPossible(restriction map[string]int) bool {
return true return true
} }
func (g Game)minKubesPower() int {
runner := map[string]int{
"red": 0,
"blue": 0,
"green": 0,
}
for _, set := range g.Sets {
for color, seenAmount := range set {
if runner[color] < seenAmount {
runner[color] = seenAmount
}
}
}
result := 1
for _, maxSeenAmount := range runner {
result *= maxSeenAmount
}
log.Printf(">>> for %+v min kubes are %+v and minPower is %n", g, runner, result)
return result
}
func ReadLine(line string) Game { func ReadLine(line string) Game {
result := Game{} result := Game{}
result.Sets = make([]map[string]int, 0) result.Sets = make([]map[string]int, 0)
@ -83,7 +105,7 @@ func ReadLine(line string) Game {
// line like this // line like this
// 8 green, 6 blue, 20 red // 8 green, 6 blue, 20 red
func readSet(setLine string) map[string]int { func readSet(setLine string) map[string]int {
log.Printf("> reading set: %s", setLine) // log.Printf("> reading set: %s", setLine)
result := make(map[string]int) result := make(map[string]int)
itemLines := strings.Split(setLine, ",") itemLines := strings.Split(setLine, ",")
for _, line := range itemLines { for _, line := range itemLines {