Conversation
| package bags | ||
|
|
||
| // Bags defines a map with the bag color as key and its family as value | ||
| type Bags map[string]bagFamily |
There was a problem hiding this comment.
There is a (minor) problem in having an exposed type containing non-exposed types.
In this case, for a bigger project, either
- expose BagFamily
- make an interface for
Bagthat something implements
| s := make(seen, 0) | ||
|
|
||
| // loop until the visit of all paths ends | ||
| for true { |
There was a problem hiding this comment.
Do you really need a loop here ? An infinite one, on top of that ?
| if !s.isSeen(target) { | ||
| s.insertSeen(target) | ||
| counter++ | ||
| counter = r.browseParents(r[target].parents, s, counter) |
There was a problem hiding this comment.
It seems to me you can start the for loop with counter++
There was a problem hiding this comment.
I'd rewrite these two (three) ifs thusly :
if !isSeen(target) {
// include this bag as seen
s.insertSeen(target)
counter++
// check for parents, and include them if any
if r.hasParent(target) {
counter = r.browseParents(r[target].parents, s, counter)
}
}| if _, ok := s[target]; ok { | ||
| return true | ||
| } | ||
| return false |
There was a problem hiding this comment.
How about return ok ?
|
|
||
| var ( | ||
| regSourceBag = regexp.MustCompile("^([a-z]+ [a-z]+) bags contain (.*).") | ||
| regContentBags = regexp.MustCompile("([0-9]+) ([a-z]+ [a-z]+) bags?") |
| counter := 1 | ||
| v := r[target] | ||
| for _, c := range v.children { | ||
| counter += c.nb * r.countBags(c.name) |
There was a problem hiding this comment.
You can also perform some memoisation and store the number of bags inside [nameX] in bag.
This would avoid having to compute twice the contents of "shiny brown bag" (and of its children)
| // CountPossiblePaths returns the number of possible bags paths to get the target bag | ||
| func (r Bags) CountPossiblePaths(target string) int { | ||
| var count int | ||
| // maintain a list of the seen bags to avoid to count duplicates |
There was a problem hiding this comment.
| // maintain a list of the seen bags to avoid to count duplicates | |
| // maintain a list of the bags seen to avoid to count duplicates |
| // and counts the number of ways to access this bag | ||
| func (r Bags) browseParents(v []bag, s seen, counter int) int { | ||
| // browse the list of parents of the target bag | ||
| for i := 0; i < len(v); i++ { |
There was a problem hiding this comment.
for _, parent := range v {
| log.Fatal(err) | ||
| } | ||
| // extract the bag name | ||
| leave := strings.Join(l[2:], "") |
There was a problem hiding this comment.
(this is the reason for the gif - you could be using l[2] instead of l[2:])
| } | ||
|
|
||
| // insertParentBag insert a parent bag into the map | ||
| func (r Bags) insertParentBag(name string, parent bag) { |
There was a problem hiding this comment.
use func (r *Bags) ...
| func (r Bags) insertParentBag(name string, parent bag) { | ||
| v, ok := r[name] | ||
| if !ok { | ||
| r[name] = newBagFamily() |
There was a problem hiding this comment.
I don't understand - this value seems (to me) to be overridden 3 lines below.

I definitely struggled a lot.
Hope to read a better cruising speed.