-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2_part2.go
50 lines (45 loc) · 854 Bytes
/
day2_part2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package day2_part2
import (
"strconv"
"strings"
)
func sumOfThePowerOfSets(games []string) int {
var err error
sum := 0
scores := map[string]int{
"red": 0,
"green": 0,
"blue": 0,
}
for _, game := range games {
sets := strings.Fields(game)
colorName := ""
colorValue := 0
scores["red"] = 0
scores["green"] = 0
scores["blue"] = 0
for i, word := range sets {
if i == 0 {
continue
}
if i == 1 {
continue
}
if i%2 == 0 {
colorValue, err = strconv.Atoi(strings.Trim(word, ":;,"))
if err != nil {
panic(err)
}
} else {
colorName = strings.Trim(word, ":;,")
if _, ok := scores[colorName]; ok {
scores[colorName] = max(scores[colorName], colorValue)
}
}
if i == len(sets)-1 {
sum += scores["red"] * scores["green"] * scores["blue"]
}
}
}
return sum
}