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!

66 Upvotes

1.2k comments sorted by

View all comments

2

u/MartinDavenport Dec 06 '20

Thought I'd have a go at day 6 part 1 in one line. It ain't pretty, but it works.

print(sum([len(i) for i in [''.join(set(i)) for i in [i.replace('\n', '') for i in open('input.txt').read().split('\n\n')]]]))

I'd be interested to see if anyone can see any more ways of cutting it down besides renaming input.txt.

3

u/StandardIssueHuman Dec 06 '20 edited Dec 06 '20

I like your approach a lot!

You can make it shorter yet by directly getting the size of each set, that is

print(sum([len(set(i)) for i in [i.replace('\n', '') for i in open('input.txt').read().split('\n\n')]]))

and even shorter than that by using set difference instead of string replacement:

print(sum(len(set(i) - set('\n')) for i in open('input.txt').read().split('\n\n')))

Edit: And if you care character count more than code style, there are three spaces you can drop from that, bringing it down to

print(sum(len(set(i)-set('\n'))for i in open('input.txt').read().split('\n\n')))

1

u/MartinDavenport Dec 06 '20

Nice one, that get it much shorter and in the process a bit more readable too!

1

u/daggerdragon Dec 06 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language.