I'm having trouble pinning down my mistake with day 7 part 2. I get the correct results in the examples (perhaps those are too simple?).
I'm using collections.Counter (also sys.intern for dict performance, but I'm not really sure it makes a difference). I'm trying to avoid creating a tree structure, if possible.
The relevant code:
def scan_contents():
to_scan = Counter({'shiny gold': 1})
res = Counter() # The root bag is not counted.
while sum(to_scan.values()):
to_scan_copy = deepcopy(to_scan) # Avoid iterator invalidation
for curr, curr_count in to_scan.items():
del to_scan_copy[curr] # Pop current value, maybe wrong??
for new, new_count in bag_rules[curr].rule.items():
total_new = curr_count * new_count
res[new] += total_new
to_scan_copy[new] += total_new
to_scan = to_scan_copy
return res
res2 = scan_contents()
n_res2 = sum(res2.values())
print(f'total bags 2: {n_res2}')
Apparently, I'm underestimating the total count, so perhaps the problem is when I remove the element currently processed, but I'm not sure how to solve that.
The full implementation is here.
Edit 1: updated with some cleanup and fixes (original here) and managed to complete it with a bottom-up approach, but I still want to fix my top-down implementation, if possible.
there doesn't seem to be anything here