Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 2.1 KB

7.md

File metadata and controls

28 lines (21 loc) · 2.1 KB

7a

Conceptually this one is pretty easy; Given a bag that contains other bags (outer, [inner]) check how often you can have a golden bag in the list of inners, where for every inner you can create an outer; (outer, [(outer, [(outer, inner)])])...

7b

This seems to be the inverse...

Concluding

Not a whole lot of comments up there. Mainly because my brain was crackling and popping trying to grapple the recursion. While conceptually it's not súperhard. Doing it in code was kind of a pain. In the first one it was simply counting the occurances, so I made a simple sort of lookup, where I flattened the list;

from:
( outer, [inner1, inner2] )

to:
(inner1, outer)
(inner2, outer)

That way, I could easily traverse 'up', checking for every inner if there is a relation to an outer, and if so, check for that outer (as inner) wether there is a relation to another outer. etc.

The second one was a bit tricky'er as I had to keep a count of everything. Doing a lot of mapping and filtering over nested arrays was tricky and my brain was struggling to keep up. There must have been a simpler solution to this...

The attack plan was similar for both cases. I wrote a 'simple' function that took a single item and went and did the operation once. Then I adjusted the input to add what I wanted to see from the output. I.e. in the second case, I wanted a count of how many bags and the last found bag. So I adjusted my input to (1, "shiny gold"). This worked nicely, because I could multiple the left hand side with the count of the other bags going deeper into the tree. But that wasn't enough, because my output would be a list, so my initial argument became [1, "shiny gold"]. Matching the input and output meant I could do this in a recursive way. The last hurdle came when you had to find in the same list over and over again. So I added a recursive wrapper to do so.

What I did like was that I kind of just wrote the parser like it was nothing. That was something I was struggling with a few days back, and went really well today!

-- I annotated ../7b.hs with quite some comments to it makes a bit more sense.