add the bag type, because number *does* matter

This commit is contained in:
Nico 2020-12-07 11:53:24 +00:00
parent ad24190da2
commit 192cf946b9
1 changed files with 16 additions and 9 deletions

View File

@ -4,21 +4,25 @@ import (
"fmt"
"io/ioutil"
"strings"
"strconv"
)
type Bags map[string][]string
type Bags map[string][]Bag
type Bag struct{
Count int
Color string}
// recursively look for a shiny gold bag
func HasShinyGold(bags Bags, b string, c []string) bool {
func HasShinyGold(bags Bags, b string, c []Bag) bool {
t := false
if len(c) == 0 { // dead end
return false
}
for _, s := range c {
if s == "shiny gold" { // end of the search
if s.Color == "shiny gold" { // end of the search
return true
} else {
x := HasShinyGold(bags, s, bags[s])
x := HasShinyGold(bags, s.Color, bags[s.Color])
if x == true {
t = true
}
@ -36,21 +40,24 @@ func main() {
bags := make(Bags)
for _, l := range lines {
halves := strings.Split(l, "contain")
colors := make([]string, 0)
colors := make([]Bag, 0)
if len(halves) < 2 {
break
}
t := strings.TrimSuffix(halves[0], " bags ")
if halves[1] == " no other bags." {
colors = []string{}
colors = []Bag{}
} else {
s := strings.Split(halves[1], ",")
for _, b := range s {
f := strings.Fields(b)
colors = append(colors, f[1]+" "+f[2])
count,_ := strconv.Atoi(f[0])
colors = append(colors, Bag{
Color: f[1]+" "+f[2],
Count: count})
}
}
bags[t] = colors
bags[t] = colors
}
total := 0
for bag, contents := range bags {
@ -59,5 +66,5 @@ func main() {
total += 1
}
}
fmt.Println(total)
fmt.Println(total)
}