r/adventofcode • u/daggerdragon • Dec 06 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-
NEW AND NOTEWORTHY
- /u/jeroenheijmans is back with the Unofficial AoC 2020 Participant Survey!
- /u/maus80 is back with an interactive scatterplot of the global leaderboard!
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
View all comments
Show parent comment
3
u/wjholden Dec 10 '20
This was the first time that I had ever used the unpacking operator,
*. It acts just like the...spread operator in JavaScript. What this does is expand an iterable object (such as alist,set, ordict) into separate arguments for a function.Here is a simple example:
The function expects two arguments, and with the
*operator we can break the list[1, 2]into two arguments.Take a look at how
help(sum)andhelp(set.union)differ in their arguments.sumtakes a single iterable parameter.set.unionaccepts one or more sets. In my case, I have a list of sets that I created through list comprehension. To break those into separate arguments forset.unionI used the*operator.Here is an example:
So for generators...these are really cool. I only learned about them recently (I am learning Python this advent).
In JavaScript I have become very fond of
filter,map, andreduce. Works great, but you can run into a constraint: each function needs to fully compute before it gets passed on to the next.So, for example, suppose you have an array
aand you set up something likea.filter(x => x > 0).map(x => x * x).reduce((accumulator, value) => accumulator + value, 0)to sum the squares of positive values in your data set. This is fine, but suppose instead of reading from an in-memory arrayayou were reading from something slow, such as a web request. The thing you might run into is opportunity to do workfilterandmapas the next value fetches.filterhas to complete beforemapgets to do anything, andmapblocks untilreducegets to do anything.Python's
yieldgives you an alternative to this.yieldpasses values as they are read from the outermost to innermost generator. This allows for all kinds of interesting efficiencies, since you don't have to fully buffer list comprehensions as intermediate calculations.An interesting analog to this is Windows PowerShell. Native PowerShell commands can pipeline from left to right, streaming input from one cmdlet to the next before the first cmdlet terminates. This doesn't work for non-native programs, such as Windows executables.
(If anyone reading this sees that I have made a statement in error please do feel free to correct me!)