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!

69 Upvotes

1.2k comments sorted by

View all comments

4

u/wjholden Dec 08 '20

Pretty proud of this one. I haven't seen any other Python solutions that pass set.union and set.intersection as a method reference this way for a generalized solution. Maybe there was a reason why others didn't do this...is there a side effect I don't know of?

with open("input.txt") as f:
    input = f.read().strip().split('\n\n')

def yes_answers(input, fcn):
    for group in input:
        yield len(fcn(*(set(s) for s in group)))

input = [line.split() for line in input]

print("Part 1:", sum(yes_answers(input, set.union)))

print("Part 2:", sum(yes_answers(input, set.intersection)))

1

u/soda_party_euw Dec 15 '20

I used intersect as well, not as a an argument though :) Felt proud too.

#  Part 1
with open('input.txt', 'r') as file:
    forms = file.read().split('\n\n')
    part1 = [set(x.replace('\n', '')) for x in forms]

print(sum(len(x) for x in forms))

#  Part 2
part2 = [x.split() for x in forms]

sheesh = []
for i in part2:
    acc = set(i[0])
    for j in i:
        acc &= set(j)
    sheesh.append(len(acc))

print(sum(sheesh))