diff --git a/day2/day-two.go b/day2/day-two.go index 09ca17e..1beb028 100644 --- a/day2/day-two.go +++ b/day2/day-two.go @@ -40,9 +40,7 @@ func Run() int { line := scanner.Text() game := ReadLine(line) log.Printf("reading %s got game %+v it is %t", line, game, game.isPossible(Restriction)) - if game.isPossible(Restriction) { - result += game.Num - } + result += game.minKubesPower() } return result @@ -60,6 +58,30 @@ func (g Game)isPossible(restriction map[string]int) bool { 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 { result := Game{} result.Sets = make([]map[string]int, 0) @@ -83,7 +105,7 @@ func ReadLine(line string) Game { // line like this // 8 green, 6 blue, 20 red func readSet(setLine string) map[string]int { - log.Printf("> reading set: %s", setLine) + // log.Printf("> reading set: %s", setLine) result := make(map[string]int) itemLines := strings.Split(setLine, ",") for _, line := range itemLines {