format part 1
This commit is contained in:
parent
a8c59173af
commit
ad24190da2
90
day7/main.go
90
day7/main.go
|
@ -1,63 +1,63 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"fmt"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Bags map[string][]string
|
||||
|
||||
// recursively look for a shiny gold bag
|
||||
func HasShinyGold(bags Bags, b string, c []string) bool {
|
||||
t := false
|
||||
if len(c) == 0 { // dead end
|
||||
return false
|
||||
}
|
||||
for _, s := range c {
|
||||
if s == "shiny gold" { // end of the search
|
||||
return true
|
||||
} else {
|
||||
x := HasShinyGold(bags, s, bags[s])
|
||||
if x == true {
|
||||
t = true
|
||||
}
|
||||
}
|
||||
t := false
|
||||
if len(c) == 0 { // dead end
|
||||
return false
|
||||
}
|
||||
for _, s := range c {
|
||||
if s == "shiny gold" { // end of the search
|
||||
return true
|
||||
} else {
|
||||
x := HasShinyGold(bags, s, bags[s])
|
||||
if x == true {
|
||||
t = true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return t
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func main() {
|
||||
// failed regex that might be worth revisiting:
|
||||
// failed regex that might be worth revisiting:
|
||||
//linere, err := regexp.Compile(`(\w+ \w+) bags contain (?:\d+ (\w+ \w+) bags?, )*?\d+ (\w+ \w+) bags?\.`)
|
||||
d, _ := ioutil.ReadFile("input")
|
||||
lines := strings.Split(string(d), "\n")
|
||||
bags := make(Bags)
|
||||
bags := make(Bags)
|
||||
for _, l := range lines {
|
||||
halves := strings.Split(l, "contain")
|
||||
colors := make([]string,0)
|
||||
if len(halves) < 2 {
|
||||
break
|
||||
}
|
||||
t := strings.TrimSuffix(halves[0]," bags ")
|
||||
if halves[1] == " no other bags." {
|
||||
colors = []string{}
|
||||
} else {
|
||||
s := strings.Split(halves[1], ",")
|
||||
for _, b := range s {
|
||||
f := strings.Fields(b)
|
||||
colors = append(colors, f[1] + " " + f[2])
|
||||
}
|
||||
}
|
||||
bags[t] = colors
|
||||
}
|
||||
total := 0
|
||||
for bag, contents := range bags {
|
||||
t := HasShinyGold(bags, bag, contents)
|
||||
if t == true {
|
||||
total += 1
|
||||
}
|
||||
}
|
||||
fmt.Println(total)
|
||||
halves := strings.Split(l, "contain")
|
||||
colors := make([]string, 0)
|
||||
if len(halves) < 2 {
|
||||
break
|
||||
}
|
||||
t := strings.TrimSuffix(halves[0], " bags ")
|
||||
if halves[1] == " no other bags." {
|
||||
colors = []string{}
|
||||
} else {
|
||||
s := strings.Split(halves[1], ",")
|
||||
for _, b := range s {
|
||||
f := strings.Fields(b)
|
||||
colors = append(colors, f[1]+" "+f[2])
|
||||
}
|
||||
}
|
||||
bags[t] = colors
|
||||
}
|
||||
total := 0
|
||||
for bag, contents := range bags {
|
||||
t := HasShinyGold(bags, bag, contents)
|
||||
if t == true {
|
||||
total += 1
|
||||
}
|
||||
}
|
||||
fmt.Println(total)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue