-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.go
59 lines (53 loc) · 1.46 KB
/
07.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
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
func main() {
input, err := ioutil.ReadFile("input.txt")
if err != nil {
panic(err)
}
bags := make(map[string]map[string]int)
lines := strings.Split(string(input), "\r\n")
for _, currentBag := range lines {
f := strings.Fields(currentBag)
color := strings.Join(f[0:2], " ")
bagsWithAmounts := make(map[string]int)
if !strings.Contains(currentBag, "no") {
for i := 3; i < len(f); i++ {
if strings.Contains(f[i], "bag") {
amount, _ := strconv.Atoi(f[i-3])
bagsWithAmounts[strings.Join(f[i-2:i], " ")] = amount
}
}
}
bags[color] = bagsWithAmounts
}
bagsRequiredInShinyGold := bagsRequiredIn("shiny gold", bags)
bagsContainingShinyGold := make(map[string]int)
bagsContaining("shiny gold", bags, bagsContainingShinyGold)
fmt.Println(len(bagsContainingShinyGold))
fmt.Println(bagsRequiredInShinyGold)
}
func bagsContaining(bagColor string, bags map[string]map[string]int, bagsHoldingBagColor map[string]int) {
for color, b := range bags {
if b[bagColor] > 0 {
if bagsHoldingBagColor[color] <= 0 {
bagsHoldingBagColor[color] = 1
delete(bags, color)
bagsContaining(color, bags, bagsHoldingBagColor)
}
}
}
}
func bagsRequiredIn(startingBagColor string, bags map[string]map[string]int) (count int) {
if len(bags[startingBagColor]) > 0 {
for color, amount := range bags[startingBagColor] {
count += amount * (bagsRequiredIn(color, bags) + 1)
}
}
return
}