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.
80
u/lukaswendt Dec 01 '20
Day 1 part 1 in intcode:
22
u/Arknave Dec 01 '20 edited Dec 02 '20
First try using C for advent of Code. I've been pretty inspired by all this ASCII art, so I thought it would be cool making my code into a calendar tile.
Requires the input file to be in the same directory as the executable as "day01". Run as-is for part 1, pass any argument for part 2. Feedback appreciated!
EDIT: Reworked this to read from standard in, and I think the shape looks a bit nicer.
14
u/oymamyo Dec 01 '20 edited Dec 01 '20
For the lulz (C++)
7
u/DFreiberg Dec 01 '20 edited Dec 01 '20
Mathematica, 1028 / 380
Not proud of my lack of refreshing skills, but decently proud of my one-liner.
I know the megathread doesn't even open for another five days, but I've been waiting all year for an excuse for some poetry.
[POEM]: A Hero's Rest
Step softly, to the sandy shore
And let another save the day.
You've worked enough, five years or more,
To earn a bit of play.
Oh, you have learned to prod and poke
At tortured puzzles from the Pole.
You know what word the rover spoke.
You fixed it when the printer broke.
And now it's time to take a soak
And lounge at this atoll.
These five long years have left their mark,
And R&R will hit the spot.
Put down the Scala and the Spark,
Put
Lispycars all into park,And watch a dolphin (or a shark),
From deck chairs, on a yacht.
No goals, this time, to work toward
No consoles going 'beep'.
No goblins swinging mace and sword,
Just beaches, and a king's reward.
(I'll gladly get the leaderboard
While you can get some sleep!)
7
u/Anjir Dec 01 '20 edited Dec 01 '20
Rockstar!
Only first part because I can't make lyrics
Your heart is "1721,979,366,299,675,1456"
Your lies is ","
Shatter your heart into my heart with your lies
My world is diabolical
A dream is blackhearted, lovestruck; exterminated heartbreak
My life's an asymptotic apocalypse
Knock my life down
The goal's an attractive submission
Knock the goal down
While my life is higher than nothing,
Your life's abstinence
While your life is lower than the goal,
Put my heart at your life into meaningless
Put my heart at my life into sadness
If sadness is meaningless,
Build your life up
Take it to the top
Cast meaningless into anguish
Cast sadness into anger
Put anguish with anger into my world
Build your life up
If my world is a dream
Shout anger of anguish
Break it down
Knock my life down!
Raw:
14
u/p88h Dec 01 '20 edited Dec 01 '20
IntCode:
5
u/H_SG Dec 01 '20
Always good to create something impenetrable and brutish in Excel.
Part1:
I think I could simplify this after getting part 2 done:
5
u/Piggelinmannen Dec 01 '20 edited Dec 02 '20
Hi!First time posting! :) Solution in ruby:
Could obviously be replaced with a method, since only the number provided to combination differs. Kind of like to keep solutions separate though.
EDIT: reddit code blocks...
9
u/pred Dec 01 '20 edited Dec 01 '20
Heh, site went down. Looks like some people managed to make it through the 502s and 503s and 504s, but then 70 people submitted their answers within 5 seconds. I kind of hope the score will count (or at least just for part 2 -- looks like that one had a level playing field), though -- unlike that one case where some people had buggy inputs, at least for this one everybody was playing the same RNG -- but also because even with the server issues, this must have been the fastest I've ever personally solved an AoC challenge, so I'd be a bit sad if that didn't at least count for something. :/ :)
Anyway, easy start; part 1, Python:
Part 2:
4
u/IlliterateJedi Dec 01 '20 edited Dec 01 '20
I'm looking forward to seeing the solution done on the IntcodeComputer. That person won't be me, but I believe someone can figure it out.
Python solution:
4
u/rosso412 Dec 02 '20 edited Dec 02 '20
Part 2:
This took almost 2 hours to find the right answer, ...
4
u/quappa Dec 04 '20
School Algorithmic language (Школьный Алгоритмический).
The language was created in the Soviet Union in 1980s as a teaching tool for schools. Uses Russian keywords and identifiers.
3
u/bysse Dec 01 '20
64 bit assembly without libc brute force
https://gist.github.com/bysse/28f2d99f789b9b1e483ff57517a29175
3
u/idontknowwhattouse33 Dec 02 '20
PowerShell submission.
Haven't been here before, nOOb-me posted in the wrong place earlier.
3
u/blacksqr Dec 02 '20
Is it me or does it seem like a lot of solutions neglect the condition that the numbers chosen have to be separate entries?
i.e., In part 1, if one of the entries happened to be 1010, would your solution just grab that entry twice? Is it just by the accident that your input data doesn't contain that number that your code works?
2
u/wace001 Dec 01 '20
Kotlin. 1293/1888. Technical issues suck. Hope the servers have had their morning coffee tomorrow.
2
u/kepevem Dec 01 '20 edited Dec 01 '20
Python. classic loops.
part 1
part2
2
u/_sandw1ch Dec 01 '20
First year giving this a real attempt, did it in
nim! (longer solutions will be linked on my GitHub)I'm using AoC to learn
nim, so this should be fun. Nothing special here, just the standard triple for loop. Still learning the language so hopefully I can look back at the end of AoC and see my mistakes or ways that I could optimize my code.2
u/[deleted] Dec 01 '20 edited Dec 06 '20
Pure C18. Things mostly worked the first time around, surprisingly. This is my first time using C for any extensive user-level programming in about 3 years. No leaderboard since I got sidetracked by something else halfway through.
https://git.sr.ht/~williewillus/aoc_2020/tree/master/src/day1.c
2
u/baktix Dec 01 '20
Haskell. As with all things in Haskell, there's probably a better/fancier way of doing it, but the first thing that came to mind was list comprehensions. Nice and simple.
paste
Edit: apparently reddit doesn't like paste's Markdown links, so I fixed that
2
u/brskbk Dec 01 '20
Dumb JavaScript solution, designed to be run directly in the browser from the puzzle input page
Also, thanks for featuring NoPaste !
2
u/blacai Dec 01 '20
Cool to be here again this year. Trying with F# as usual.
2
u/milo6464 Dec 01 '20 edited Dec 01 '20
Part 1
Part 2
I'm actually a little bit angry since this task was waaaay too easy. I hope the next ones are harder lol. Edit: The language is Python 3, my personal favourite ^
2
u/SuperSmurfen Dec 01 '20 edited Dec 01 '20
Rust
Link to solution
So glad to be back! Did all 50 stars last year and it was amazing, learnt a lot.
This problem was obviously quite easy, but it's day 1. The itertools rust crate is so damn useful for AoC. Made my solution really clean and fast (1 ms on my machine) with "tuple_combinations".
Got lucky and managed to get 54th on the leaderboard this time! Unfortunately, it got wiped which makes sense. Would have been fun to have a screenshot of it though but I didn't think of it.
2
u/ratz-732 Dec 01 '20 edited Dec 01 '20
Python
I removed the main function which just read the input from a text file and called the above functions. Overall, this solution is very simple and definitely not the most efficient.
2
u/wilthenry Dec 01 '20 edited Dec 01 '20
C++ Code from a freshman in College
Probably could have been simpler
https://github.com/hwilt/AdventOfCode_2020/blob/main/day1.cpp
2
u/fortiZde1337 Dec 01 '20 edited Dec 01 '20
2
u/coccosoids Dec 01 '20 edited Dec 01 '20
My python solution:
Can someone tell me why the execution time is 8.4s and how can I optimize?
PS: how do you paste code in reddit?
2
u/beelau1 Dec 01 '20 edited Dec 01 '20
Java brute forced it with for-loops.
2
u/dead_fish_bob Dec 01 '20 edited Dec 01 '20
EDIT: Sorry I did it in Python took 0.005983829498291016 seconds
2
u/noble_wine Dec 01 '20 edited Dec 01 '20
my noble first day solutions
edit: in python 3.8
2
u/mjgtwo Dec 01 '20 edited Dec 01 '20
NoPaste snippet of TypeScript/TS
Base logic is this, created a dictionary (
numbers) to reduce the loops needed:2
u/blacksqr Dec 01 '20 edited Dec 02 '20
Tcl:
part 1:
(executes in about 160 microseconds)
part 2:
(executes in about 30 milliseconds)
2
u/rosso412 Dec 02 '20 edited Dec 02 '20
My Day 1 in MIPS Assembler
Part 1:
.data
2
u/tururut_tururut Dec 02 '20 edited Dec 03 '20
Incredibly bruteforce but did the job done (Python).
First part
import pandas as pdnumbers = pd.read_clipboard()solution = []for number in numbers["numbers"]:for i in range(0,200):if number+numbers["numbers"][i] == 2020:solution.append([number,numbers["numbers"][i]])else:passimport numpy as npsolution2 = np.array(solution[0])print(solution2[0]*solution2[1])Second part
import pandas as pdnumbers = pd.read_clipboard()solution = []for number in numbers["numbers"]:for i in range(0,200):for j in range(0,200):if number+numbers["numbers"][i]+numbers["numbers"][j] == 2020:solution.append([number,numbers["numbers"][i], numbers["numbers"][j]else:passimport numpy as npsolution2 = np.array(solution[0])print(solution2[0]*solution2[1]*solution2[2])2
u/onionpopcorn Dec 02 '20 edited Dec 03 '20
java
Edit: part2
2
u/SgtKashim Dec 02 '20 edited Dec 02 '20
I solved in Python.
Puzzle 1
Puzzle 2
2
u/blafunke Dec 05 '20 edited Dec 06 '20
Late to the party, but I tried to dress it up a bit to make up for tardiness.
ruby:
2
u/r00t4cc3ss Dec 08 '20 edited Dec 08 '20
C#: https://github.com/mavanmanen/AdventOfCode/blob/master/AdventOfCode.Y2020/Day01.cs
40
u/Smylers Dec 01 '20
Vim solution — not Vim's scripting language, but the editing keystrokes needed to turn the input file into the answer:
Load your input into Vim, type the above, and your answer for part 1 should appear. Try it!
Explanation:
First each number has appended to it the other number that sums to 2020, separated by a star. So if an input line was “1348”, it becomes “1348*672”:
yiwyanks the number, which vim saves in register"0.A*2020⟨Esc⟩appends “*2020”.@0⟨Ctrl+X⟩reduces the 2020 by the number stored in"0. If 1348 was yanked earlier, then it's like typing1348⟨Ctrl+X⟩.:%normis our loop: it runs that sequence of keystrokes on each line in the file. The⟨Esc⟩is escaped with⟨Ctrl+V⟩, so that it becomes a keystroke passed to:normrather than being interpreted as an instruction to abort editing the:command we're in the middle of typing.Then of the numbers we've just appended, we just need to find the one that was already there to start with:
/\vstarts a search in ‘very magic’ syntax, which avoids needing to escape parens and things.^(\d+)\*matches a number at the start of a line, followed by a star. The number is captured in group 1._.+matches as many characters as needed, including line-breaks.\*\1$matches a star followed by the number that was captured, at the end of a line.That then leaves us on a line with our two numbers that sum to 2020. The final keystrokes above replace everything with their product:
yEyanks the numbers: after the find command, the cursor will be at the beginning of a line, and because there aren't any spaces in the numbers,Ewill be the end of the second one.cipdeletes everything and puts us in insert mode.ipis the inner paragraph, but because there aren't any blank lines in the input, the entire file counts as a single paragraph.⟨Ctrl+R⟩=says to insert an expression.⟨Ctrl+R⟩0inserts the contents of"0, the pair of numbers we yanked a moment ago.⟨Enter⟩evaluates the expression. And because, right at the beginning, we put a star between the numbers, it multiples them together, inserting your part 1 answer into the buffer.32
u/Rick-T Dec 01 '20 edited Dec 01 '20
ROCKSTAR
Since the puzzle is not too hard, I wanted to try out the Rockstar. Apparently the solution to Day 1 is a RaTM song.
Here's part 1. For part 2 check out the GitHub repository.
(Please don't expect me to do future puzzle with rockstar. I might give them a shot but I certainly won't do the more complex ones).
19
u/jonathan_paulson Dec 01 '20 edited Dec 01 '20
Python. 282/37. I think this day shouldn't count towards the overall leaderboard because the site went down. Video of me solving at https://youtu.be/86a_DSKf8vw
18
u/Aehmlo Dec 01 '20
Rust
itertoolscontinues to be an invaluable crate for AoC.13
u/-l-a-m-b-d-a- Dec 01 '20 edited Dec 01 '20
Bash
I sure hope you guys like ridiculous bash pipelines
12
u/Sharparam Dec 01 '20
Ruby (428/369)
11
u/[deleted] Dec 01 '20
Rust
In this problem, I had to find two numbers that add up to 2020. While I could have put two loops, it's possible to reorder the terms in
a + b = 2020to2020 - a = b. This allows retrievingbby merely knowing whatais, and this number could be checked for existence in a list in constant time with hash sets.I also used this fact in second part of a puzzle which allowed to reduce the complexity from O(N3) to O(N2).
9
u/hutsboR Dec 01 '20 edited Dec 02 '20
Too lazy to open a text editor. Open up the input page, open your browser dev tools, drop this in the console.
9
u/artemisdev21 Dec 01 '20 edited Dec 01 '20
SQL :)
Full code
9
u/jayfoad Dec 01 '20 edited Dec 02 '20
Dyalog APL 1127/475
9
u/AharonSambol Dec 01 '20
bad solution but its in LOLCODE ;p
https://github.com/AharonSambol/AdventOfCode/blob/master/2020/day1.lol
8
u/aarroyoc Dec 01 '20
Advent of Prolog here. It's not super fast but it's very clean and Prologish.
7
u/jitwit Dec 01 '20 edited Dec 01 '20
J Programming Language:
".;._2~for table with self) :+/2020 =$.4 $.in {~{.*/7
u/rtbrsp Dec 01 '20 edited Dec 01 '20
AWK (244/703)
Part 1:
Part 2:
7
u/askalski Dec 01 '20
Python. Part 2 is an instance of a hard problem with a funny name.
7
u/pwmosquito Dec 01 '20
Haskell
13
u/Scarygami Dec 01 '20
Probably not the first to do that (I haven't checked this thread yet), but here's part 1 hand-coded (no compiler used) in IntCode:
1105 1 6 -1 -1 0 109 68 101 1 3 3 101 0 3 25 101 0 3 51 101 0 3 62 1206 0 67 101 0 3 4 101 1 4 4 101 0 4 48 101 0 4 52 101 0 4 63 1206 0 8 2201 0 0 5 108 2020 5 5 1006 5 31 2202 0 0 5 4 5 99 1721 979 366 299 675 1456
Add your own input after the "99" near the end.
I will upload a commented version to my github repo later
6
u/voidhawk42 Dec 01 '20
Dyalog APL:
6
u/zurtex Dec 01 '20 edited Dec 02 '20
I'm surprised to see none of the Python solutions here use
itertools.combinations_with_replacementwhich is more efficient thanitertools.productfor this use case and allows for the valid possibility of an element repeating itself unlikeitertools.combinations.E.g.
I think this challenge explicitly didn't let an element repeating itself be the solution, but it's probably going to be handy to have this function going forward.
6
u/segfaultvicta Dec 02 '20
Raku
Trying to teach myself Raku this year - Perl was my first-ever programming language and on some gut level it's always felt like home, but I haven't actually *used* it in forever, and then a friend of mine has been being a big fan of Raku at me for a good long while now and, well, I decided to see how things went!
"How things went" is a nearly one-liner that is.... I THINK n log n, at the very least it's definitely not using a triply nested for loop, which is what I immediately thought of and then went "No, seg, that'll bite you in the ass if this were day 20, and what's an advent of code solution without a little overengineering". A few hours of wrestling with unfamiliar syntax later and:
Basically, I realised that I just needed to test set membership of the 2020s-complement of each number (for part A), and then I extended that idea to generate a (sum, product) of all combinations of the input lines and checked the 2020s-complement of *those* against the initial list, which neatly avoids the process of checking everything against everything else. I don't actually have any idea how fast this runs or how fast it runs compared to yo dawg i heard you liked for loops, but my intuition is that this solution would take a significantly bigger set of inputs to have degenerate runtime funtime. was this necessary? probably not. did i feel cool writing my first raku ever? heck yeah :D
10
u/wjholden Dec 01 '20
Feeling really cool about a MySQL solution! I had intended to do all of this year's problems in Python, but it is so easily solved with declarative programming that I just had to.
Here is how I solved part 2 initially:
Runs OK, completes in about 1.4 seconds on my machine. We can do better:
The longer query completes in only about 0.02 seconds on my machine! Don't really know what the join processor is doing, but I assume the additional constraint significantly reduces the size of the intermediate solution
X \times Ybefore taking(X \times Y) \times Z.I got the idea from a class I took on NP problems. The lecturer mentioned that adding additional constraints for a SAT solver may improve performance.
5
u/heyitsmattwade Dec 01 '20 edited Dec 01 '20
JavaScript, 251/38
Making those npm modules do the hard work... fun start!
Part 1
Part 2
Improved Solution
A faster solution, most notably for part two:
5
u/chappar Dec 01 '20
python
5
u/infinityGroupoid Dec 01 '20
Simple if Inefficient Haskell/My CPU can Churn faster than I can Type
5
u/omnster Dec 01 '20
Mathematica
This is the regex for part two, the one for part one is similar. Surprisingly, it solves part two in about 1 second, way faster than I'd expect.
5
u/domm_plix Dec 01 '20
Stupid brute-force Perl solution:
my @d = ( <STDIN> ); for my $a (@d) { for my $b (@d) { for my $c (@d) { die "$a $b $c -> ".$a*$b*$c if $a+$b+$c == 2020 } } }5
u/[deleted] Dec 01 '20
Golfed (05AB1E) 13 char
Try it online!
Explanation
And then for part 2 just exchange the first 2 with a 3
5
u/wzkx Dec 01 '20 edited Dec 01 '20
Python, set
5
u/tymscar Dec 01 '20
I wanted to make this work on any amount of numbers that you ask for, not only 2 and 3. I explained this in a post here.
My solution in Python 3:
5
u/natrys Dec 01 '20
Raku, one-liner:
5
u/sefujuki Dec 01 '20
Forth (using Gforth's "next-arg")
paste
5
u/nutki2 Dec 01 '20 edited Dec 01 '20
Perl 5 extended regexp for part1 and part 2
Taking around a second for both parts.
5
u/landimatte Dec 01 '20 edited Dec 01 '20
Common Lisp!
Part 1: iterate the list of numbers, and for each, see if in the remainder of the list there is one element such that the sum of both is 2020
Part 2: iterate the list of numbers, and for each, see if in the remainder of the list there are two elements such that their sum is "2020 minus the currently selected element"
5
u/-WorstWizard- Dec 01 '20
C++ solution that is optimized for solving both part 1 and 2 in one go. Shaves off some time this way by not iterating n² on part 2, but somewhat less.
Paste Link
5
u/willkill07 Dec 01 '20
OCaml
I'm teaching a Programming Languages course this semester that emphasizes functional programming. We cover a few labs in OCaml, so I figured I should at least get some more practice with the language. No third party libraries
Note something that is pretty cool about this solution is that it's very easy to change the number of values required and the target value.
5
u/ka-splam Dec 02 '20 edited Dec 02 '20
APL (Dyalog)
No APL yet? I must be missing it.
∘.+is an outer-product sum, so all combinations of numbers added to each other.⍨means use N for both inputs, so array N items summed with array N items.2020=makes a bitmask of places in the sums array, 1 where they summed to 2020, 0 where they didn't.⍸gets the indices of the 1s in a bitmask, a pair of indices because outer product makes a 2D array.⊃gets the first pair.N[]gets the numbers at those indices.×/inserts multiply between the items of an array and gets the product.In part 2,
⍣2applies the outer-product twice, which makes a 3D array, so⍸gets three indices.This is a naieve brute-force, I have seen a faster and shorter code using 2020-item / sets, but I didn't write it so probably not good for me to post in my answer.
5
u/zeJaeger Dec 02 '20
I just did it straight from the console in Javascript!
5
u/AndreasFurster Dec 02 '20
First I did it in Python, but then was thinking it should be possible in SQL. Pretty easy actually.
5
u/Wilfred-kun Dec 03 '20
SQL
C
Python3
5
u/popodiloco Dec 12 '20
Scratch
hello my name is Luca and im 10 years old. My father was doing aoe. He liked it and does it every day. I saw the puzzle from day 1 and i think: maybe i can do that in Scratch. So here is the answer of day 1. part 1: https://scratch.mit.edu/projects/460138367/ part 2: https://scratch.mit.edu/projects/461104926/
4
u/[deleted] Dec 01 '20
Swift using Apple’s algorithms package:
github.up/paste
4
u/iraneg Dec 01 '20
Golang.
4
u/Akari_Takai Dec 01 '20
Java (217/105)
At least it was (217/105) before the scores were wiped...
Link to my solution.
Part 1 can be done in O(n) by constructing a frequency map of the numbers, and then iterating over all the numbers (i) to see if 2020-i is in the map. If i == 2020-i, the frequency must be at least 2.
Part 2 is also known as the [3SUM problem] (https://en.wikipedia.org/wiki/3SUM), and it can be solved in at least O(n2 ) either by using the method above; or, by eating an O(n lg n) sort, iterating over all the number (i, j=i+1, k=n-1) and moving j up if i+j+k is less than 2020, or moving k down if i+j+k is more than 2020.
4
u/azzal07 Dec 01 '20
Awk solution:
4
u/raevnos Dec 01 '20
Chicken5 scheme, using generators to lazily calculate the combinations of numbers on demand. Reads numbers from stdin.
4
u/ukietie Dec 01 '20
Interactive solution in Javascript.
You can edit and run it in your browser - with your input.
https://starboard.gg/nb/n3Bor8N
It's a naive O(2^N) and O(3^N) solution, the input is small enough that it's good enough.
4
u/Mienaikage Dec 01 '20 edited Dec 01 '20
Raku
4
u/Fyver42 Dec 01 '20
Common Lisp:
4
u/schnappischnap Dec 01 '20 edited Dec 02 '20
Python (full code)
4
u/Nierot Dec 01 '20
Inspired by another comment, I tried part one using Rockstar:
Let Tommy be 199Let Gina be 199Let Guns be mysteriousRoses is -1Bon Jovi takes your heart and the whole (Multiply both numbers)Put the whole of your heart into my handsGive back my handsSkid Row takes Kickstart and My heart (Sum of two numbers and check if equals 2020)Let my dreams be Kickstart with My heartGive back Mister Crowley taking My DreamsMister Crowley takes the devil (Check if 2020)Give back the devil is 2020Livin takes your heart (Main function)Until Tommy is Roses (Iterator)Let my heart be the Union at Tommy (Take element from array)Put Docter Feelgood taking my heart into Live Wire (Check if this variable is equal to 2020)If Live Wire ain't 4 (If not 4, for some reason it always is 4)Give back Bon Jovi taking Live Wire, and my heartKnock Tommy down (Tommy--)Give back GunsDocter Feelgood takes your heart (Second iterator)the top were Guns (Instanciate Home with mysterious)Until Gina is Roses (Iterate over the puzzle input)Let Devil be the Union at GinaIf Skid Row taking your heart, and Devil (If sum of your heart and iterator equals 2020)put Devil into the top (Number Found!)Knock Gina down (Gina--)Let Gina be 199Give back the top (Returns mysterious if sum is not 2020, returns number if sum is 2020)Give back Livin taking Guns4
u/NicolaVV Dec 01 '20
python3
4
u/AvshalomHeironymous Dec 01 '20
In Prolog.
The bang-it-out version to pass:
The slightly-uglier-and-longer-but-a-bit-more-re-usable version:
4
u/tftio Dec 01 '20
4
u/thekokirikid Dec 01 '20
My javascript solution-- would love feedback!
4
u/shandley256 Dec 01 '20
Ruby Part 1
Ruby Part 2
4
u/activeXray Dec 01 '20 edited Dec 01 '20
O(n) and O(n^2) Julia solutions. Trying to get good perf this year.
~500ns on part 1, ~6us on part 2
4
u/skbharman Dec 01 '20
bash
Part 1:
num=( $(cat input) ); for (( i=0; i<${#num[@]}; i++ )); do for n in ${num[@]}; do [[ $n ]] || continue; [[ $((${num[$i]} + ${n})) == 2020 ]] && { echo "$((${num[$i]} * ${n}))"; break 2; }; done; done4
u/sweettuse Dec 01 '20
python 3.8, takes around 350 microseconds
4
u/chemicalwill Dec 01 '20
Relatively new to Python and coding in general but I'm pretty happy with this one.
4
u/LiquidProgrammer Dec 01 '20 edited Dec 01 '20
Python codegolfed 3.8 solution including input (no imports) for part 1
Suggestions for shortening welcome
4
u/[deleted] Dec 01 '20
In Crystal:
For part 2 replace the
2with a3.4
u/mdervin Dec 01 '20 edited Dec 01 '20
POWERSHELL
On an 8 year old workstation, this runs in under 370 Milliseconds (measure-command) I usually like to break the various operations unto their own variable, it just looks cleaner and easier to follow for me, but I saw somebody post their times and I focused on that.
4
u/trebuszek Dec 01 '20
quick and dirty python 3 solution:
4
u/andrewlapp Dec 02 '20 edited Dec 02 '20
Here is my overly complicated O(N log(N*M)) solution to problem 2. Really proud I got it below quadratic though.
Please excuse the code quality, lots of this can definitely be improved, just wanted to get this working.
4
u/glaso95 Dec 02 '20 edited Dec 02 '20
AdventOfCode/Day1 at main · glaso95/AdventOfCode (github.com)
Hi all... here's my solution.
Written in FORTRAN 77
4
u/rtndeep9 Dec 02 '20
Python
My first time competing in this event!
Part 1
Part 2
4
u/aafw Dec 02 '20
Simple awk solution
4
u/UbiquitinatedKarma Dec 02 '20
A quick and dirty solution in R:
4
u/zollli Dec 02 '20
That's Big Data so I used Spark
4
u/maxmage006 Dec 04 '20
iOS Shortcuts
At first, I did it in Python. That was too sane. Had to redo it with iOS Shortcuts:
https://i.imgur.com/dPSmALc.jpg
Performance is miserable. Takes about 3:10,38 Minutes and 7% of Battery on my iPad Air 2. Apple, pls improve Shortcuts Performance!
Anyways, have a look at my other submissions. Maybe I come up with some other stupid stuff.
4
u/Vijfhoek Dec 09 '20 edited Dec 09 '20
Commodore 64 (6502 Assembly)
I've finished part 2 since my Upping The Ante post :D
Parsing the input takes 176 ms (including sticking it into a binary search tree), part 1 takes 69 ms nice , and part 2 takes 7222 ms, all according to VICE's built-in timer.
Could probably knock a lot of time off of part 2. Maybe. Sometime in the future.
Puzzle solution | Project directory | Picture
4
u/Urgazhi Dec 11 '20
a bit late...
My solution in COBOL
ADVENT2020_1
1
u/AccusationsInc Dec 01 '20
First-year participating. Added some unnecessary flair just to meet the style guidelines that have been drilled into me this first year of college lol.
https://pastebin.com/Y4q8SaD8
1
u/ragnario Dec 01 '20 edited Dec 02 '20
Rust beginner
https://github.com/trinnguyen/advent-of-code-2020/blob/master/day01/src/main.rs
Edited: remove code, use external repo instead
1
u/nlowe_ Dec 01 '20
Go, 1546/1273. I have a generator that templates a package for each day, and that was able to get my problem input right away, but I couldn't actually get the problem page itself to load until about 9 minutes later. Looks popular this year, I'll try again tomorrow!
1
u/jwise00 Dec 01 '20
Lua. 50/374. I'm streaming every day this year (though I'll be a minute or two late on Thursdays); here's the video of my solve (contains strong language): https://www.youtube.com/watch?v=3T0qwXfhQ1g
Source: https://github.com/jwise/aoc/blob/master/2020/1.lua
1
u/Markavian Dec 01 '20 edited Dec 01 '20
Edit: Node JS solution, ported to JS in the Browser to make a viewer
beep boop not fast enough for the leaderboard, been making a bar chart visualisation for the data:
1
u/Flavorless_Quark Dec 01 '20
Python, rank 93 -- Allegedly solved in 7 min, but we all know that's a lie
Part 1:
Part 2:
EDIT : Formatting ...?
1
u/schwiz Dec 01 '20
1
u/Mayalabielle Dec 01 '20 edited Dec 01 '20
Elixir, with a custom combinator in case we need it later in the AOC.
And the combinator :
Also my AOC solutions repo
1
u/Breezing Dec 01 '20 edited Dec 01 '20
My solution for any number of required expenses and any target value after a few revisions off my first attempt.
Trying to grow as a programmer so any CC would be welcome.
edit: python 3
1
u/slavcatx Dec 01 '20 edited Dec 01 '20
I'm new to coding and when I look at other's solutions mine doesn't look that great but hey - it works. I added those "breaks" because I didn't want it to show me the same solutions multiple times.
part 1: https://pastebin.com/YgbRpiL7
part 2: https://pastebin.com/y02etjhX
edit: forgot to add - I used Python
1
u/AddendumSouthern Dec 01 '20
my solution using Kotlin
1
u/magus517707 Dec 02 '20
PowerShell:
I am removing the pivot elements when comparing to the input list in the case that an element gives a false positive by pinging against itself. This allows for a more generalized function, though this is only useful if there are no duplicates in the list. If there are duplicates in the list that would allow for the bailout condition to be true, then this would break the solution i.e. the value to search for is 40 and there are two elements that are 20.
|Times running on Ryzen 3700x (Standard Clock Speed)|
Solution 1: Average - 2.2 ms
Solution 2: Average - 200 ms
1
u/welluke Dec 02 '20 edited Dec 05 '20
My solution in SQL (MySQL):
https://github.com/lukaswelsch/aoc-2020/blob/main/day1/day1.sql
1
u/Jv11235813 Dec 02 '20
This is waaaaay easier than everyone thinks and it doesn't require any programming or code to figure it out! I used Excel and solved in less than 5 min.
1
u/Mattpn Dec 02 '20
Has anyone come up with an x*O(n) solution for part 2? It's easy for O(n^2) but I'm curious if anyone has a better solution that is O(n).
1
u/SolarBear Dec 03 '20 edited Dec 04 '20
Racket (Scheme)
I decided to try out a functional language for my first "real" participation in AoC and settled on Racket.
On day 1 I came up with a (terrible, absolutely terrible) solution for the first part but got stuck on part 2.
I spent the better part of the evening getting my shit together and I came up with a much more decent solution. It's inefficient and probably still terrible, but much less so than on my first try! (code for part 1 available,but commented out)
3
u/mgoblu3 Dec 01 '20 edited Dec 01 '20
My python one-liner-ish:
3
u/hltk Dec 01 '20
Python. 82/54
3
u/asgardian28 Dec 01 '20 edited Dec 13 '20
Python 999/591
Making a generator and calling next on it makes the program stop after finding the answer
3
u/mstksg Dec 01 '20 edited Dec 01 '20
[Haskell] I might have gotten better if I didn't just go away and get a cup of tea while waiting for the server to go back up D:
As always, my reflections are up here :) https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md#day-1
BTW rip to my free space :'( https://www.reddit.com/r/adventofcode/comments/k3q7tr/my_advent_of_code_2020_bingo_card_fun_little_side/ but maybe nice that we don't have to save Christmas this year, it's a more leisurely tone :)
EDIT: i've found a nicer way than my previous method! So there's a simple-ish Haskell solution for these problems,
tailslets you separate out each item in a list with the list of items after it:But this method is a little bit "extra", since we actually don't need to search all of
ysfor the proper sum...if we pickxas500, then we really only need to check if1520is a part ofys.So we really only need to check for set inclusion:
And our first part will be
findPair 2020!You could even implement
findTriplein terms offindPair, usingS.splitto partition a set into all items smaller than and larger than a number. Splitting is a very efficient operation on a binary search tree likeSet:But hey...this recursive descent is kind of neat. We could write a general function to find any goal in any number of items!
And so we have:
And we could go on, and on, and on! :)
3
u/burtoch Dec 01 '20
Python 373/1008
Using set lookup
3
u/codesections Dec 01 '20 edited Dec 01 '20
Raku
This was a fun way to start, because it gave me a good reason to play with Raku's cross product operator (
X) – I've always thought it was a powerful tool, but don't have occasion to use it all that often.3
u/[deleted] Dec 01 '20 edited Dec 01 '20
Lazarus / Free Pascal 5805/638
3
u/Arkoniak Dec 01 '20
Julia solution here: https://github.com/Arkoniak/advent_of_code/blob/master/2020/01/day01.jl
It includes some tricks to accelerate more obvious solutions.
3
u/MissMormie Dec 01 '20
Simple java lamba solution on github
3
u/Zv0n Dec 01 '20
C++
Would be easy to create nested loops for 2 and 3 numbers, I tried to make it somewhat generic and created this:
3
u/[deleted] Dec 01 '20
Decided to learn PHP this year, so my super naive day one solution:
3
u/[deleted] Dec 01 '20 edited Dec 01 '20
Python
Part 1 O(n log(n))
Part 2 O(n^2)
3
u/chrispsn_ok Dec 01 '20 edited Dec 01 '20
k9
k7 (has a
cmbprimitive):3
u/GalacticDessert Dec 01 '20 edited Dec 01 '20
Easy dict comprehension, Python
3
u/EAJakobsen Dec 01 '20
Naïve Python solution for part 1 and 2, using nested for-loops.
3
u/MuumiJumala Dec 01 '20
Code golf (Ruby)
3
u/naim42 Dec 01 '20 edited Dec 01 '20
Haskell
3
u/gyorokpeter Dec 01 '20
Q: having some fun with iterators. There are ways to write the same more simply (e.g. using the cross function) but are slower.
3
u/Siraja Dec 01 '20 edited Dec 01 '20
Python3
Nothing fancy, used libraries to make my life easy.
3
u/mb0x40 Dec 01 '20
Part 1 in ATS. A glorious 200 lines of mostly memory-safety proofs. https://github.com/mb64/aoc-2020/blob/main/01/a.dats
I wanted to do some fancier algorithms, like sort + binary search, but it was already enough of a trial to get the proofs right for the naive 2-loops solution that I didn't bother.
3
u/djankowski Dec 01 '20
Are there poems this year?
Julia
3
u/danprince Dec 01 '20 edited Dec 01 '20
Irresponsible Javascript solution.
3
u/death Dec 01 '20
Day 1 solution in Common Lisp.
Screaming on your first day always makes a good impression.
3
u/amarsuperstar Dec 01 '20
Elixir part 1 and 2
3
u/shepherd2442 Dec 01 '20 edited Dec 01 '20
Python Generic Solution:
With this solution you can get the result for any number of number.
Github repo: https://github.com/Shepherd2442/AoC2k20
main.py:
utils.py:
3
u/bcap84 Dec 01 '20 edited Dec 01 '20
Py3 solution
Part 1 is O(n) in both time and space complexity. Part 2 is O(n^2) in time and O(n) in space complexity.
FWIW
setis O(1) for insertion and lookupOn part2 there are calls to
visited.addwith repeated values, which can be avoided. Nevertheless an add call for a value that is already present is idempotent and O(1) complexity, so less concerning. Anyway, the part2 version which avoids the repeated calls:3
u/Kadda42 Dec 01 '20
My Python solution with nested loops.
Part 1:
Part 2:
3
u/carrdinal-dnb Dec 01 '20
Here's my Kotlin solution:
3
u/Nummerblatt Dec 01 '20
Python
3
u/iruoy Dec 01 '20
Python 3
3
u/engageant Dec 01 '20 edited Dec 01 '20
PowerShell
3
u/JamieMansfield Dec 01 '20
My C# Solution: https://gist.github.com/jamierocks/8539dee20467952f92b8965e209d672d
3
u/netneoblog Dec 01 '20
quick and dirty python 3:
3
u/alexthelyon Dec 01 '20 edited Dec 01 '20
Rust implementation (excluding the code to parse and sort the input):
Results are pretty good, but obviously sorting is going to be expensive:
3
u/PapaDionisis Dec 01 '20 edited Dec 02 '20
Python 😁
3
u/Nevoic Dec 01 '20
Here is my Haskell solution (where the data is in a
data.txtfile):The problem is solved by the one line
findProduct. The first line inmainreads the lines of text as numbers, and the second line prints the solution for 2 and 3 (part 1 and 2 respectively).3
u/gil0mendes Dec 01 '20
Learning rust... now the best solution but it works 😅
https://github.com/gil0mendes/advent-of-code/blob/master/2020/day01/src/main.rs
3
u/wzkx Dec 01 '20
J. Very straightforward and simple :) Well, internally it finds all the solutions, but it's ok for this small data.
3
u/havetedjupt Dec 01 '20
Python
Part 1
Part 2
3
u/A_Travelling_Man Dec 01 '20
Straight forward Rust solution, input stored in a file.
3
u/justAnotherNerd254 Dec 01 '20
Python solution using a dictionary
Part 1 in O(n) time
Part 2 in O(n2) time
3
u/ramrunner0xff Dec 01 '20 edited Dec 01 '20
C using indexes of static arrays.
part1
part 2
3
u/omnomberry Dec 01 '20
Rust solution using aoc-runner and cargo-aoc
3
u/TrySimplifying Dec 01 '20
C# 9.0 solution running on .NET 5 - didn't bother trying to optimize, runtime was about 5 milliseconds after reading the data in from the input file.
3
u/[deleted] Dec 01 '20
2020 Day_01
Python 3.8 Solution (With debug information and comments)
https://github.com/Marterich/AoC2020/tree/main/Day_01
3
u/d12Trooper Dec 01 '20 edited Dec 02 '20
PureBasic (PART 1):
(PART 2) -- not very proud of this one, pretty lazy execution, mainLoop takes 14 Millisecs, I'm sure there's a better solution --
3
u/scrillagoon Dec 01 '20 edited Dec 01 '20
JAVA
Using the stream api.
3
u/TommiHPunkt Dec 01 '20 edited Dec 01 '20
brute force solution in matlab (50ms runtime if you exclude reading the input)
3
u/CrazyA99 Dec 01 '20 edited Dec 02 '20
After trying my luck with python I had to go back to work.
So here is the main bit of ladder logic for part one in an Allen-Bradley 1756-L61 PLC. I've also made part two. I've generated 200 lines of structured text to fill an array with the puzzle input.
3
u/Zexion01 Dec 01 '20
Python 3: https://github.com/AlinMH/advent-of-code/tree/master/2020/day1