The Problem
We have to combine all the single-character responses from groups of people in part 1, and count only the unique responses in part 2.
My Solution
Thankfully, a bit of sanity. I recognized both these as set
s so just used the union
and intersection
operators. Quite happy with my input parsing as well — split into groups based on nn
One problem that I faced was that the last group from part 2 was not being processed properly. The last line was empty, hence its intersection with the rest of the data was zero. Learnt the difference between split("n")
and split()
Implementation
- A few solutions did what I would have blindly done — manually count unique solutions. Some also combined this with
set
s! - We can use a single function and pass it set.reduce and set.union. cool! This function can apply the arguments using
reduce
, or, even cooler, using the*
operator - Once we have the unpacking operator, a one-liner with a combination of set and list comprehensions is possible. Interesting, but ugly code, and defeats the purpose….
Overall interesting problem, but not too challenging. I learnt a few new things about python, but no new algorithmic ideas.