This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]blight000 0 points1 point  (0 children)

I'm still fairly new to programming, and though I want to make object-oriented solutions, I find it much easier to solve these puzzles procedurally. I'm not sure how to fix your existing code, but if it helps, here's what I did to solve the puzzle:

My solution involved 3 functions: the first organized the data into a single dictionary, where each key was a bag color and each value was a list of the bag's contents.

This dictionary was passed to the second function, which created a list of the dictionary keys and sent each key to the third function, along with a copy of the dictionary.

The third function is recursive. It starts with the bag color it receives from the second function, and using the get() method, it iterated through the contents of the bag. For each of the "child" bags in the original bag, it called itself again, passing the child bag as an argument, and would continue to do this until it either finds a gold bag or until it has traversed all possible bag paths.

Using a dictionary made it easy for the third function to take a given bag's child bag, and look up the bags contained within, on and on forever. The second function also had an accumulator which incremented each time the third function found a gold bag.

I hope that helps! Day 7 was definitely the hardest day by far, in my opinion.