My Python solution for Advent of Code 2020 day 6. My initial working version was much more verbose than the copy below, but the tools used were the same - set and collections.Counter.
Very happy with my decision back during day 3 to split up puzzle input retrieval from parsing - it’s made testing the puzzle examples using the same code paths as testing my actual input much easier, which in turn makes me more likely to actually write those tests.
fromcollectionsimportCounterfrompathlibimportPathimportunittestdefget_raw_input():return(Path(__file__).parent/'day_06_input.txt').read_text()defparse_raw_input(raw_input):return[[[charforcharinperson.strip()]forpersoningroup.splitlines()]forgroupinraw_input.strip().split('\n\n')]classGroup():def__init__(self,input):self.people=[personforpersonininput]@propertydefflattened_questions(self):return[questionforpersoninself.peopleforquestioninperson]@propertydefunique_questions(self):returnset(self.flattened_questions)@propertydefquestions_answered_by_all(self):counter=Counter(self.flattened_questions)return[questionforquestion,countincounter.items()ifcount==len(self.people)]defpart_one(parsed_input):returnsum([len(Group(group).unique_questions)forgroupinparsed_input])defpart_two(parsed_input):returnsum([len(Group(group).questions_answered_by_all)forgroupinparsed_input])if__name__=='__main__':print(f'Part one: {part_one(parse_raw_input(get_raw_input()))}')print(f'Part two: {part_two(parse_raw_input(get_raw_input()))}')classTestPartOneExamples(unittest.TestCase):deftest_example_one(self):example="""abcx abcy abcz"""self.assertEqual(part_one(parse_raw_input(example)),6)deftest_example_two(self):example="""abc a b c ab ac a a a a b"""self.assertEqual(part_one(parse_raw_input(example)),11)classTestPartTwoExample(unittest.TestCase):deftest_example(self):example="""abc a b c ab ac a a a a b"""self.assertEqual(part_two(parse_raw_input(example)),6)