format part 2
This commit is contained in:
parent
5430cd0fed
commit
57f3d98b91
15
day7/main.go
15
day7/main.go
|
@ -3,14 +3,15 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Bags map[string][]Bag
|
||||
type Bag struct{
|
||||
type Bag struct {
|
||||
Count int
|
||||
Color string}
|
||||
Color string
|
||||
}
|
||||
|
||||
// recursively look for a shiny gold bag
|
||||
func HasShinyGold(bags Bags, b string, c []Bag) bool {
|
||||
|
@ -33,14 +34,14 @@ 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)
|
||||
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 += b.Count * mul
|
||||
s += CountBags(bags, b, b.Count*mul)
|
||||
}
|
||||
return s
|
||||
|
@ -65,9 +66,9 @@ 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],
|
||||
Color: f[1] + " " + f[2],
|
||||
Count: count})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue