diff --git a/day7/main.go b/day7/main.go index b4fe825..556c895 100644 --- a/day7/main.go +++ b/day7/main.go @@ -3,14 +3,15 @@ package main import ( "fmt" "io/ioutil" + "strconv" "strings" - "strconv" ) type Bags map[string][]Bag -type Bag struct{ - Count int - Color string} +type Bag struct { + Count int + Color string +} // recursively look for a shiny gold bag func HasShinyGold(bags Bags, b string, c []Bag) bool { @@ -33,17 +34,17 @@ func HasShinyGold(bags Bags, b string, c []Bag) bool { } 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 + 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() { @@ -65,13 +66,13 @@ func main() { s := strings.Split(halves[1], ",") for _, b := range s { f := strings.Fields(b) - count,_ := strconv.Atoi(f[0]) + count, _ := strconv.Atoi(f[0]) colors = append(colors, Bag{ - Color: f[1]+" "+f[2], - Count: count}) + Color: f[1] + " " + f[2], + Count: count}) } } - bags[t] = colors + bags[t] = colors } total := 0 for bag, contents := range bags { @@ -80,6 +81,6 @@ func main() { total += 1 } } - fmt.Println(total) - fmt.Println(CountBags(bags, Bag{Color: "shiny gold"}, 1)) + fmt.Println(total) + fmt.Println(CountBags(bags, Bag{Color: "shiny gold"}, 1)) }