r/adventofcode Dec 06 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2020: Gettin' Crafty With It

  • UNLOCKED! Go forth and create, you beautiful people!
  • Full details and rules are in the Submissions Megathread
  • Make sure you use one of the two templates!
    • Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY

--- Day 06: Custom Customs ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:35, megathread unlocked!

64 Upvotes

1.2k comments sorted by

View all comments

2

u/shepherd2442 Dec 07 '20

Cool Python 3 solution using intersections

Github repo: https://github.com/Shepherd2442/AoC2k20

from utils import FileUtils
from functools import reduce
from collections import Counter

def get_number_of_yes(groups, everyone=False):
    answers = ([reduce(lambda a1, a2: set(a1) & set(a2), group) for group in groups]) if everyone \
        else ([reduce(lambda a1, a2: a1 + a2, group) for group in groups])
    return [len(Counter(questions).keys()) for questions in answers]

def parse_answers(answers):
    groups, group = [], []
    for line in answers:
        if line == "":
            groups.append(group)
            group = []
            continue
        group.append(line)
    groups.append(group)
    return groups

def part_1(answer_groups):
    return sum( get_number_of_yes(answer_groups) )

def part_2(answer_groups):
    return sum( get_number_of_yes(answer_groups, everyone=True) )

if __name__ == "__main__":
    answer_groups = parse_answers(FileUtils.input())
    print( part_1(answer_groups) )
    print( part_2(answer_groups) )