format part 2
This commit is contained in:
parent
5430cd0fed
commit
57f3d98b91
43
day7/main.go
43
day7/main.go
|
@ -3,14 +3,15 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bags map[string][]Bag
|
type Bags map[string][]Bag
|
||||||
type Bag struct{
|
type Bag struct {
|
||||||
Count int
|
Count int
|
||||||
Color string}
|
Color string
|
||||||
|
}
|
||||||
|
|
||||||
// recursively look for a shiny gold bag
|
// recursively look for a shiny gold bag
|
||||||
func HasShinyGold(bags Bags, b string, c []Bag) bool {
|
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 {
|
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]
|
c := bags[bag.Color]
|
||||||
if len(c) == 0 { // dead end
|
if len(c) == 0 { // dead end
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
var s int
|
var s int
|
||||||
for _, b := range c {
|
for _, b := range c {
|
||||||
s += b.Count*mul
|
s += b.Count * mul
|
||||||
s += CountBags(bags, b, b.Count*mul)
|
s += CountBags(bags, b, b.Count*mul)
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -65,13 +66,13 @@ func main() {
|
||||||
s := strings.Split(halves[1], ",")
|
s := strings.Split(halves[1], ",")
|
||||||
for _, b := range s {
|
for _, b := range s {
|
||||||
f := strings.Fields(b)
|
f := strings.Fields(b)
|
||||||
count,_ := strconv.Atoi(f[0])
|
count, _ := strconv.Atoi(f[0])
|
||||||
colors = append(colors, Bag{
|
colors = append(colors, Bag{
|
||||||
Color: f[1]+" "+f[2],
|
Color: f[1] + " " + f[2],
|
||||||
Count: count})
|
Count: count})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bags[t] = colors
|
bags[t] = colors
|
||||||
}
|
}
|
||||||
total := 0
|
total := 0
|
||||||
for bag, contents := range bags {
|
for bag, contents := range bags {
|
||||||
|
@ -80,6 +81,6 @@ func main() {
|
||||||
total += 1
|
total += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(total)
|
fmt.Println(total)
|
||||||
fmt.Println(CountBags(bags, Bag{Color: "shiny gold"}, 1))
|
fmt.Println(CountBags(bags, Bag{Color: "shiny gold"}, 1))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue