Part One
Easy peasy.
Part Two
Definitely fleeced me a bit. I think I spent an hour and a half on this or something.
Originally, though I thought I read the question very carefully, I made a really big error.
The way I originally added up the cards was like this. Let’s say the number of matches/wins we have per card is [2, 1, 0, 0, 0, 0]
I iterated through the cards this way:
starting state
[2, 1, 0, 0, 0, 0]
Card 1
[2, 2, 1, 0, 0, 0]
Card 2
[2, 2, 2, 1, 0, 0]
Card 3
[2, 2, 2, 2, 1, 0]
Card 4
[2, 2, 2, 2, 2, 1]
See this pattern? I am conflating the matches
with the copies
, and using the same data structure to represent them. But they really aren’t the same.
MATCHES determine how far the additionally copies are distributed farther down the array. By default, they distribute 1
additionally copy for every match that exists, even if that current card doesn’t have any copies to its name.
COPIES on the other hand are a totally different species with different implications. They also add additional cards down the stack, but they don’t do so in ‘distance’ but rather in quantity. The same cards will be getting copies distributed to them, but the # of copies +1
of the current card determines how many copies get distributed to them (where 1 is the default that still gets distributed, even if there are no copies, only the original).
I completely conflated these two data structures into a single one, which really didn’t work.
There is also sort of a third data structure, which is the total number of instances of cards we get in the end, but it’s debatable whether this really gets its own data structure, as it is simply the same as if you took the copies
data structure and added 1
to every value.
I am getting some code smells that we really only may need one data-structure to handle all of this, but that might be a trap of perfectionism. I don’t think more data structures is worse, as long as we are reasoning through and trying to understand the problem - it’s probably better.
Meta Gotcha
I think the main gotcha I experienced was not respecting the natural distinctions of data that were given to me. The words copy
and card
were not conflated by the example, and were delivered to me as two distinct entities. But my silly human brain thought of them as the same thing, as the same information, and that very conflation led to a state of collapsed of information was not a foundation that a working algo could stand on.