solve part 2

This commit is contained in:
Nico 2020-12-07 12:53:55 +00:00
parent 192cf946b9
commit 5430cd0fed
1 changed files with 15 additions and 0 deletions

View File

@ -32,6 +32,20 @@ func HasShinyGold(bags Bags, b string, c []Bag) bool {
return t
}
func CountBags(bags Bags, bag Bag, mul int) int {
fmt.Printf("#%v\t%d\n",bag, mul)
c := bags[bag.Color]
if len(c) == 0 { // dead end
return 0
}
var s int
for _, b := range c {
s += b.Count*mul
s += CountBags(bags, b, b.Count*mul)
}
return s
}
func main() {
// failed regex that might be worth revisiting:
//linere, err := regexp.Compile(`(\w+ \w+) bags contain (?:\d+ (\w+ \w+) bags?, )*?\d+ (\w+ \w+) bags?\.`)
@ -67,4 +81,5 @@ func main() {
}
}
fmt.Println(total)
fmt.Println(CountBags(bags, Bag{Color: "shiny gold"}, 1))
}