r/adventofcode • u/daggerdragon • Dec 01 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 1 Solutions -🎄-
It's been one heck of a crappy year, so let's make the holidays bright with Advent of Code 2020! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!
We're following the same general format as previous years' megathreads, so make sure to read the full description in the wiki (How Do the Daily Megathreads Work?) before you post! If you have any questions, please create your own thread and ask!
Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!
[Update @ 00:04] Oops, server issues!
- /u/topaz2078's working on it.
- In the meantime, hop over to Twitch and listen to our live DJ Veloxxmusic!
[Update @ 00:06]
- Servers are up!
[Update @ 00:27]
[Update @ 01:26]
- Many thanks to our live deejay Veloxxmusic for providing the best tunes I've heard all year!!!
NEW AND NOTEWORTHY THIS YEAR
- Created new post flair for
Other - When posting in the daily megathreads, make sure to mention somewhere in your post which language(s) your solution is written in
COMMUNITY NEWS
Advent of Code Community Fun 2020: Gettin' Crafty With It
- Last year y'all got real creative with poetry and we all loved it. This year we're gonna up our own ante and increase scope to anything you make yourself that is related to Advent of Code. Any form of craft is valid as long as you make it yourself!
- Full details, rules, timeline, templates, etc. are in the Submissions Megathread.
- Several folks have forked /u/topaz2078's
paste(source on GitHub) to create less minimalistic clones. If you wishedpastehad code syntax coloring and/or other nifty features, well then, check 'em out! - /u/brskbk - NoPaste
- GitHub user EthanJohnson -
pastefork on GitHub - /u/Tranzystorek - fork of EthanJohnson's fork on GitHub
--- Day 1: Report Repair ---
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.
View all comments
Show parent comment
4
u/askalski Dec 02 '20
Polynomial multiplication. Watch what happens when you multiply
(x^2 + x^5) * (x^7 + x^11). Applying the "FOIL" rule, you get:Notice how you pair up terms of the first polynomial with terms from the second, then add the exponents together. (The coefficients are multiplied, though I didn't demonstrate it. More on that later.)
Now, think of
(x^2 + x^5)as the list of numbers2, 5, and(x^7 + x^11)as the list7, 11. The above operation of multiplying the polynomials can be thought of as "pick one number from each list, then add them together". You can then read the product(x^9 + x^12 + x^13 + x^16)as the list of possible sums:9, 12, 13, 16.The puzzle asks us to pick two (or three) numbers from the same list. That's analogous to squaring (or cubing) a polynomial. For example, let's see what happens when we pick two numbers from the list
2, 3, 7:So the list of possible sums is:
4, 5, 6, 9, 10, 14. Furthermore, look at the resulting coefficients.x^4has a coefficient of1because there is exactly one way to get a sum of 4:2+2. Similarly,x^5has a coefficient of2because there are two ways to get a sum of 5:2+3and3+2. Note that, because of this symmetry, I divide the Part 1 coefficient in half. (And in Part 2, I divide by six because there are 6 ways to order the three numbers.)Finally, remember how I mentioned that when you multiply two polynomials, each pair of coefficients is multiplied together. This is convenient, because the answer we're looking for is the product of the two (or three) numbers that add up to 2020. We just need to make one small change to the original polynomial; instead of
x^n, we writen x^n:The coefficient of
x^10is 42 because 10 is3+7and7+3, and the sum of the products is:3*7 + 7*3 == 21 + 21 == 42This trick relies on a couple important assumptions:
1010+1010or1010+500+500)One thing I should point out is that
xis merely a symbol that you manipulate algebraically, and doesn't represent any concrete value.If you're interested in learning more, this is an application of a combinatorial tool called a "generating function". Two (free!) books on the topic are Analytic Combinatorics (Flajolet/Sedgewick) and generatingfunctionology (Wilf).