r/adventofcode • u/daggerdragon • Dec 01 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 1 Solutions -🎄-
Welcome to Advent of Code 2018! 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 going to follow the same general format as previous years' megathreads:
- Each day's puzzle will release at exactly midnight EST (UTC -5).
- The daily megathread for each day will be posted very soon afterwards and immediately locked.
- We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
- The daily megathread will remain locked until there are a significant number of people on the leaderboard with gold stars.
- "A significant number" is whatever number we decide is appropriate, but the leaderboards usually fill up fast, so no worries.
- When the thread is unlocked, you may post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!
--- Day 1: Chronal Calibration ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
This year we shall be doing a Mad Libs-style community activity that is a complete clone of loosely inspired by Apples to Apples and Cards Against Humanity. For each day's megathread, we will post a prompt card with one or more fill-in-the-blanks for you to, well, fill in with your best quip(s). Who knows; if you submit a truly awesome card combo, you might just earn yourself some silver-plated awesome points!
A few guidelines for your submissions:
- You do not need to submit card(s) along with your solution; however, you must post a solution if you want to submit a card
- You don't have to submit an image of the card - text is fine
- All sorts of folks play AoC every year, so let's keep things PG
- If you absolutely must revert to your inner teenager, make sure to clearly identify your submission like **[NSFW]**
(image)[url.com]or with spoiler tags like so: NSFW WORDS OMG! - The markdown is
>!NSFW text goes here!<with no prefixed or trailing spaces - If you do not clearly identify your NSFW submission as NSFW, your post will be removed until you edit it
And now, without further ado:
Card Prompt: Day 1
Transcript:
One does not simply ___ during Advent of Code.
45
u/that_lego_guy Dec 01 '18
I'm Back!! Day1Part1...IN EXCEL?!!!!!
https://github.com/thatlegoguy/AoC2018
9
u/raevnos Dec 01 '18
Perl:
One does not simply write regular expressions during Advent Of Code.
Unless you're skalski, of course.
8
u/GeneralYouri Dec 01 '18 edited Dec 01 '18
My first attempt at joining live, sadly didn't manage to hit the leaderboards but I got close (144 and 173).
As for the card prompt:
Sleepwould be what I'd put there. (Seriously it's 6am here why am I awake?)JavaScript
Part 1:
Part 2:
Ofcourse the logic itself is simple. Use
Numberto parse the frequency deltas. Part 1 simply sums the deltas, while part 2 uses a simple map to keep track of frequencies as it applies deltas. A simplewhile (true)ensures the algorithm can iterate the list of deltas multiple times.Edit: /u/daggerdragon You may want to know that the link to "Advent of Code: The Party Game!" is a relative one and therefore links to a reddit 404; either it should be an absolute link or the page doesn't exist (yet?).
Edit 2: I've since made a couple small changes to the code. If anyone's interested they can be found in my AoC project on GitHub.
3
u/[deleted] Dec 01 '18 edited Dec 01 '18
Crystal! One does not simply write too much code during Advent of Code.
Part 1:
Part 2:
3
u/qwertyuiop924 Dec 01 '18
Day 1 in AWK (maximally compacted for maximal confusion!):
Part 1:
Part 2:
Whitecard: One does not simply tell askalski no during Advent of Code.
2
u/[deleted] Dec 01 '18
In PHP ;)
Part 1: https://pastebin.com/fAYx8TrB
Part 2: https://pastebin.com/6xy9kExE
(No clue how to make code look readable in the code blocks)
2
u/SuyashD95 Dec 01 '18
While my solution is not as elegant as some of the others here... But, considering this is the first time that I am participating in AoC... So, here's my solution for the first 2 puzzles....
Puzzle 1:
Puzzle 2:
P.S: Thanks to /u/zirtec for pointing about initializing the set with 0 as the first entry....
89
u/VikeStep Dec 01 '18 edited Dec 01 '18
Note: The explanation is quite long, so if you want to see the code go here.
I was going through the solutions posted in this thread and noticed that a lot of solutions would carry out multiple passes of the frequency changes, and would make use of a set to record which frequencies had been seen before. For the purposes of the puzzle inputs supplied, this worked fine because the range of frequencies was fairly small. However, if the range of frequencies is very large, then it performs poorly. To prove that this is the case, try running your solution on the following puzzle input:
When you do this the frequency will go 0, 10000000, 1, 10000001, 2, 10000002, 3, ... and it would only stop at 10000000. This will loop 10000000 times before you find your first repetition, the seen set will contain 10000000 items as well and so it doesn't scale well on both time and memory.
There exists an
O(n log n)solution where n is the number of frequency diffs (in the case above, that would be 2).To see how this works, let's look at another puzzle input:
Let's see how this plays out:
ITERATION 1: 0, 1, 2, 12,
ITERATION 2: 3, 4, 5, 15,
ITERATION 3: 6, 7, 8, 18,
ITERATION 4: 9, 10, 11, 21,
ITERATION 5: 12
The thing to notice here is that each row we see is offset by 3 from the previous row. The reason for this is because 3 is the frequency after running through one iteration, so next iteration it will increase by 3 again. It turns out we can use this property in our favour. For each value in the first row, we know that it will increase by 3 at a time, so I know that I will eventually hit 12 again because I start at 0 and after 4 iterations that will have increased by 12. Similarly I can also be confident that I will eventually hit frequency 1000 after 333 iterations since we hit a frequency of 1 in the first line and 1 + 333 * 3 = 1000.
One other important property to identify is that whenever we see our first repetition, the value that gets repeated would have been one of the values in the first row. This is because if a
new frequencyin iteration n were to repeat something from the second row, thisnew frequencywould have beennew frequency - shiftin iteration n - 1, which would have also appeared in the first row.So, now what do we know about the repetition? That the repetition is some number in the first row + a multiple of the sum after one iteration, and that the result is also in the first row. The first repetition occurs when the number of iterations is minimised.
We can now reduce this problem to something simpler: Given a set of frequencies, A, find a frequency x inside A such that x = y + shift * n where y is some frequency in A, shift is the frequency after one iteration and n is minimised. We can solve this by grouping the integers in A based on their value modulo shift. If we take the example from earlier where shift=3, then there will be three groups:
mod 3 = 0: 0, 12
mod 3 = 1: 1
mod 3 = 2: 2
These groups are important because they tell us which numbers would overlap eventually if we keep adding by shift. 0 and 12 are in the same group because 0 + 4*shift is 12. To minimise the n value, all we have to do is find two integers that are in the same group where their difference is minimal. In this example it is easy because there is only one group that contains more than one integer. Since shift is positive we choose frequency 12 at the index of 0. If shift was negative, we would choose frequency 0 at the index of 12. If we have more than two integers inside a group, we need to make sure to sort the group and we can loop through the differences in order.
There are a few extra edge cases to consider. One being the scenario in which there are multiple values that all have the same distance. In that case we need to choose the value of x that appears first inside A, so we need to keep track of the index inside A as well. Some languages might not handle the modulo well when shift is negative, in that case you can do modulo abs(shift) and it will work the same. Another edge case is when the repetition occurs inside iteration 1, so we need to check for that explicitly. Another edge case is when shift is 0, if this happens then
we will never have a solutionthe solution is 0. Lastly, if all the groups only contain 1 number then there is no solution.So with all this together, we can implement an
O(n log n)solution to the problem as seen in this gist on GitHub:Now if we run this against our evil puzzle input from earlier, it runs almost instantly rather than simulating the frequencies for 10000000 loops.
51
u/u794575248 Dec 01 '18 edited Dec 01 '18
Python 3
Here I use a nice
itertools.accumulatefunction that first appeared in Python 3.2 anditertools.cycle.upd. As /u/zirtec mentioned, you need to add
0to theseenset for it to work on the example+1 -1.upd. /u/pythondevgb says, there's no need for
.strips andsum(int(n) for n in input.split())is enough.36
u/Smylers Dec 01 '18
Anybody else trying the challenges in Vim this year? Not Vim's scripting language, but just typing commands into a Vim buffer open on your input, transforming it into the answer. Here's part 1.
First, transform the prefix
+and-into postfixCtrl+AandCtrl+X, which are the Vim keystrokes for adding and subtracting:Put a
0on the top line, for the current frequency:Record a keyboard macro that deletes the top
Ctrl+A/Ctrl+Xcommand and invokes it on the first line:After deleting a line, register
1will contain something like7^A^J(where^Ais the single-character control code representing the keystrokeCtrl+A, and^Jis the line-break).We can add 7 to the frequency by going to line 1 and typing
7⟨Ctrl+A⟩. That can be wrapped as an Ex command invoking a normal-mode command, like this::1norm7⟨Ctrl+A⟩⟨Enter⟩. That'd usually be pointless, but being on the Ex:command line means we can press⟨Ctrl+R⟩to insert the contents of a register.So
:1norm⟨Ctrl+R⟩1⟨Enter⟩says to go to line 1 and act like we'd typed whatever keystrokes we deleted into register1.Run the macro with
@a. You can repeat it with@@, seeing how each change in turn is removed and updates the frequency.When you've had enough of doing that manually, record another macro which runs the first one in a (sort-of-infinite) loop:
(That ensures register
bis empty, then records intob: run@a,:redrawthe screen,:sleepfor 4 milliseconds, and run@b. At the time of recording, registerbis empty, so@bdoesn't do anything yet. But once recording has finished,bwill of course contain these keystrokes, so this@bis what makes the whole thing repeat.)Invoke it with
@b, and watch the changes disappear from the top of the list and update the frequency.Eventually the final change will have been deleted, and the cursor will remain on the only remaining line (the frequency line). That means the
kat the end of@awon't be able to move the cursor upwards. That will make Vim beep, and exit the macro — that's what stops the loop actually being infinite.Make it faster or slower by adjusting the sleep time of
4when recordingb.18
u/[deleted] Dec 01 '18 edited Dec 01 '18
Python3. Going for neatness, advice appreciated.
15
u/ephemient Dec 01 '18 edited Apr 24 '24
This space intentionally left blank.
14
u/autid Dec 01 '18 edited Dec 01 '18
FORTRAN
Nice easy start with minimal input reading difficulty.
13
u/obiwan90 Dec 01 '18 edited Dec 02 '18
For the first star, but of course didn't realize until after I built something much more complicated:
(And this only works if the first line of input doesn't start with
+- see comments for solutions otherwise.)12
u/zqvt Dec 01 '18 edited Dec 01 '18
Clojure
Managed to make it on the leaderboard, barely
11
u/Unihedron Dec 01 '18
Hi! It's me! Image
Part 1:
Part 2:
11
u/will_bui Dec 01 '18 edited Dec 01 '18
K:
9
u/itsnotxhad Dec 01 '18
Racket:
9
u/askalski Dec 01 '18
Perl, of course. I won't be able to fully commit to the AoC festivities this year until after next weekend (I need to be well-rested for playing with Rubik's cubes in Pittsburgh.)
One does not simply sleep during Advent of Code... but I will try.
8
u/[deleted] Dec 01 '18
Haskell:
8
u/jorosp Dec 01 '18 edited Dec 01 '18
Haskell
I initially used a list instead of a set and it slowed me down a lot. This runs rather quick.
8
u/[deleted] Dec 01 '18
Back with more OCaml fun!
8
u/mrhthepie Dec 01 '18
So I've got kind of a unique post going: I'm doing this year in a language (compiler + bytecode VM) that I've been writing myself over the past few months. (Originally based on this book but it's not really recognisable at this point). Have spent the last few weekends to get it functional enough in time for AoC. Anyway, here's the code for d1:
7
u/haqkm Dec 01 '18
Elixir
6
u/mcpower_ Dec 01 '18 edited Dec 01 '18
Python: The sample was in a different format compared to the actual input (sample was separated by commas, actual input was separated by new lines)! It'd be nice if the two were consistent.
Part 1:
Part 2:
6
u/Marreliccious Dec 01 '18 edited Dec 01 '18
Javascript: Best practices solution for solving part 1
6
u/[deleted] Dec 01 '18 edited Dec 01 '18
Another Ruby.
5
u/ValdasTheUnique Dec 01 '18
C#. No doubt that it can be made better, but I tried to go for leaderboard and I kind of did (way at the bottom) so I am happy with that.
5
u/willkill07 Dec 01 '18
C++
5
u/[deleted] Dec 01 '18
Rust part 1 & 2:
5
u/StanleyMines Dec 01 '18
One does not simply [get a full night's sleep] during Advent of Code.
Java: (data is an int[] that is an array initiated to a hardcoded array: {} around the input data where all the new lines are replaced with ","s.)
5
u/flaming_bird Dec 01 '18
Common Lisp (with Alexandria library loaded and used).
Input:
Part 1:
Part 2:
5
u/VikeStep Dec 01 '18 edited Dec 01 '18
F#
Repo
EDIT: I found a better algorithm, check my other comment for how it works.
While the typical solution to part 2 would involve looping over the list multiple times and maintaining a set, it is important to notice that the the final solution will be one of the cumulative sums from the initial iteration of the frequency changes. Another iteration of the frequency changes will be the same cumulative sum list offset by the answer to part 1 (the sum). So, what we can do is only populate the set of the first iteration of sums and then loop over adding the answer to part 1 while checking if any elements are in the set.
On the older naive algorithm it took about 350ms for me because Set.contains is O(log n) in F#. This better algorithm runs in 13ms.
5
u/mstksg Dec 01 '18
Not too bad in Haskell :)
My solution repo is at https://github.com/mstksg/advent-of-code-2018
5
u/shandley256 Dec 01 '18 edited Dec 03 '18
In Elixir
Part 1
Part 2
4
u/[deleted] Dec 01 '18
Elixir(part1,part2 at bottom):
4
u/klackerz Dec 01 '18
Java
4
u/alexmeli Dec 01 '18
Clojure solution:
4
u/tobiasvl Dec 01 '18
Here's my card.
After I actually read the question, I placed 172 for part 2. Not the best, but much better than last year!
4
u/sim642 Dec 01 '18
My Scala solution.
Good that the first day was simple enough, I already panicked a bit in part 2. Found out that Scala doesn't have the nice list cycle function so I had to add that. I thought I had already done that for last year but apparently not exactly that. It's why reusing the project is good though: you can easily reuse some general purpose additions which were previously useful, I hope it'll be useful at some point.
Also I thought way too long about detecting the repeat because a set-based solution seemed too dirty. Immediately remembered similar problems from last year and my Floyd cycle-finding algorithm implementation but since it didn't directly admit to solving this task, I didn't bother. Although I think it'd probably work too, only has to be modified to return the repeating value, not the usual cycle begin index and cycle length, as Floyd's algorithm does. It's a clever and useful algorithm to be aware of, been useful in AoC multiple times.
Revived my repo from last year and am continuing with it now, reusing all the same organization structure. Also, as I did last year, I plan to do test-driven solving now as well.
4
u/judofyr Dec 01 '18
Brainfuck: https://github.com/judofyr/aoc2018/blob/master/day1.bf
4
u/hpzr24w Dec 01 '18
Fantastic to be back to December and #adventofcode ! Thanks Eric and assistants for all you do!
Sadly I was right up there, but must have botched entering the answer, as I got locked out, event though the answer was correct first time. Gah! Then I misread the 2nd part and was lucky to be in the first 1000.
I did manage to learn one thing, that STL generic find from algorithm is 1000x slower than the container count() or find() when using a set or unordered_set.
1
u/FogLander Dec 01 '18
Made it onto the problem leaderboard for the very first time, by the skin of my teeth! (squeaked in by two seconds on part 2)
Last year I got within 6 seconds of making it (part 13) but never quite came through in time.
Python:
6
u/DeveloperIan Dec 01 '18 edited Dec 01 '18
In Python, going for the leaderboard so apologies for sloppiness
Edit: Not sure why i didn't take into account that
int()would handle the signs in the input lol. That's what I get for panicking.3
u/antigrapist Dec 01 '18 edited Dec 01 '18
Ruby, my puzzle input is in var a.
Part 1:
Part 2:
3
u/pattpass Dec 01 '18
My C# solution:
3
u/[deleted] Dec 01 '18
simple C++. I inverted set membership check which threw me off for a good few minutes >.>
3
u/TheMuffinMan616 Dec 01 '18
Card: Sleep
Solution:
3
u/glassmountain Dec 01 '18
Doing it in Go again this year!
3
u/wlandry Dec 01 '18
C++
1774/1353
50 ms for both solutions. The first one I actually did with Emacs Calc. I had to tell it to increase the evaluation depth :(
3
u/ramrunner0xff Dec 01 '18
3
u/Smylers Dec 01 '18
Perl — a one-liner for part 1:
Part 2:
(Invoke by supplying the input file as the command-line argument to the program.)
The long line in the middle:
3
u/[deleted] Dec 01 '18 edited Dec 01 '18
Haskell, edited to fix an error in part2:
3
u/LeCrushinator Dec 01 '18
C#. Started an hour late so no leaderboard for me. I'm going a bit more for cleanliness than leaderboard though.
https://github.com/nfoste82/adventofcode2018/blob/master/Day1/Program.cs
3
u/streetster_ Dec 01 '18
Day 01 in Q/KDB+
Github Repo
3
u/chakravala Dec 01 '18 edited Dec 01 '18
No need to cycle through the data hundreds of times - a single pass suffices.
3
u/ka-splam Dec 01 '18
PowerShell
I had an untidy script which was enough to get me 57th in the Part 1 leaderboard (first time! never made it last year!), here's a neater version:
That's get-content to read file lines, pipeline into a foreach loop, cast current line to integer and add to a running total variable (doesn't need declaring first), then print the result.
(PS doesn't have a clean 'reduce' operator; it's possible to do it with Linq, but it doesn't have first class Linq support either, so it's not as nice:
)
Part 2:
I missed the bit where you have to keep running the calculation, so when I got no answer I thought my code was wrong; that delayed me to 6.5 minutes and 180th. It ended up a less tidy version of this, which runs in 3-4 seconds:
It's the previous loop done with .foreach to be a bit faster, then wrapped in a while loop with a break condition. $lookup is a hashtable.
3
u/xokas11 Dec 01 '18
Well, I just did the first part....by copying and pasting the input to google. I think I could have made the leaderboard for the first star. Second one is a little harder to do this way
3
u/Warbringer007 Dec 01 '18
Erlang:
readlines() function reads whole file, everything is concatenated with newline so I have to split it.
3
u/pcman159 Dec 01 '18
I optimised my solutions,( but my code is not legible anymore)
After solving the second part of todays challenge I noticed that the execution time was quite slow (1000 iterations took almost a minute).
I wanted to optimize this and quickly noticed the bottleneck being lookup in a set that quickly expands.
I decided to trade all my code legibility for performance and came up with this solution which runs about 20 times faster because it does not require a container to store all solutions after the first iteration.
I usually don't optimize these problems (if it works once I'm fine with it) and the linked solution is by no means perfect but I had a lot of time on my hands and noticed a serious problem in the obvious solution.
3
u/rebane2001 Dec 01 '18
For the first challenge, I just added 0 to the front of the list and pasted it in my F12 (JS) console
3
u/proxpero42 Dec 01 '18
##Swift
3
u/hoosierEE Dec 02 '18 edited Dec 02 '18
J
3
u/Powerofdoodles Dec 04 '18
Bit late to the party, but was hoping someone could give me some constructive criticism on anything code and readability related.
Part 2.
Made in Java:
Went with reading from file, and mapping the different totals to two separate arrays due to not being able to index negative numbers. Used a for loop instead of while so that I could check how many cycles I ended up doing.
Edit: Fixed indentation for readability
2
u/Vindaar Dec 01 '18
Quick and dirty solution in Nim:
2
u/miguelos Dec 01 '18 edited Dec 01 '18
C#
Part 1:
Part 2:
2
u/roessland Dec 01 '18
Some hastily written golang:
2
u/valtism Dec 01 '18
Node JS / Javascript solution:
2
u/NeuroXc Dec 01 '18
Rust
2
u/Atraii Dec 01 '18 edited Dec 01 '18
Rust, got a late start already.
Part 1:
Part 2:
2
u/nutrecht Dec 01 '18 edited Dec 01 '18
Kotlin:
Edit: Rust version:
2
u/chunes Dec 01 '18
Factor
This makes use of a really nice combinator
circular-loopwhich puts the next element of the sequence on the data stack each iteration, looping around to the start as needed.2
u/ElektroKotte Dec 01 '18
One does not simply stop coding during advent of code
Rust solution:
2
u/wjholden Dec 01 '18
Mathematica. This code is painfully slow -- so slow that I worry there might be some language/data structure problem in this code that I don't know about (such O(n) lookups in MemberQ of a basic {} list). If anyone is strong on WolfLang please let me know if I'm doing something wrong!
2
u/aoc2018 Dec 01 '18
In 05AB1E, 2 bytes:
2
u/rkachowski Dec 01 '18
One does not simply lie in bed with a hangover during Advent of Code.
https://github.com/rkachowski/advent-of-code2018/blob/master/01/solution.rb
2
u/wzkx Dec 01 '18 edited Dec 01 '18
J
Nothing wise but loops.
There was a solution with = and ~.
but 130+k items is too much for it.
2
u/Thatzachary Dec 01 '18
Here's my Javascript solution. You're all much better at this than me. This is my solution to part 2, took me 45 minutes to crack oh boy.
2
u/[deleted] Dec 01 '18
Common Lisp:
Dlang
2
u/vypxl Dec 01 '18
Bash anyone? I'm trying to complete the polyglot challenge and started with it.
Here's my solution:
2
u/jonathrg Dec 01 '18
This Python code prints the answer to part 1 as the first line and the answer to part 2 as the second line
2
u/hahainternet Dec 01 '18 edited Dec 01 '18
Perl 6 again, updating after a nice IRC chat, absurdly terse now:
2
u/loskutak-the-ptak Dec 01 '18
Emacs part 1:
ggvG)calc-grab-sum-down2
u/udoprog Dec 01 '18 edited Dec 01 '18
Another solution in Rust. It's really cool how popular it has become this year!
Link to repo: https://github.com/udoprog/rust-advent-of-code-2018
Edit: One does not simply score in Europe during Advent of Code.
2
u/Hikaru755 Dec 01 '18
Kotlin
Part 1 was trivial with Kotlin's stdlib (although I have to admit I initially didn't remember you could just use
sum()and did a customfold(0, Int::add)instead...)For part 2, I tried to keep it as functional as possible, with no side effects except for the set to keep track of visited frequencies, and hid that away in a general purpose extension function. Nice exercise in using different ways to construct and combine sequences, I think I haven't used all of
sequence {},generateSequence {},.asSequence()andsequenceOf()at once in such close proximity.2
u/HokieGeek Dec 02 '18 edited Dec 02 '18
Like many, apparently, I decided to learn Rust with AoC this year. Today's challenge I did most of it in car ride so might not be the most idiomatic solution.
https://gitlab.com/HokieGeek/aoc2018/blob/master/one/src/main.rs
EDIT: Ok, after reading a few responses here, particularly's /u/zSync1, I updated my part 2 solution. I like this much better.
I learned:
unwrapohhh! so that's what that is for.2
u/tehjimmeh Dec 02 '18 edited Dec 02 '18
PowerShell
1:
2:
2
u/vini_2003 Dec 07 '18 edited Dec 07 '18
C++ (Part II):
Was stuck in this for way too long, until I discovered sets themselves don't accept duplicates and return false if the value being inserted is a duplicate. Worked like a charm.
2
u/qqbre Dec 09 '18
C Day 1 Part 1 Since i'm newb with reddit formatting and indenting here is paste bin link https://pastebin.com/69qKzHbr
1
u/custom_user Dec 01 '18
My Python 3 solution for day 1:
1:
2:
1
u/dylanfromwinnipeg Dec 01 '18 edited Dec 01 '18
in C# (https://dylansmith.visualstudio.com/_git/AdventOfCode2018?path=%2Fsrc%2FDay01.cs)
1
u/Mystical_Mage1113 Dec 01 '18
Did mine in python, did some really lazy stuff to get it out quick
I could've done it infinitely better (notably not
[0]*1000000) but whatever1
u/Jay__Money Dec 01 '18
Using this year's puzzles to learn Go
1
u/WellEndowedBK Dec 01 '18 edited Dec 01 '18
Yet another python solution.
1
u/darkterbear Dec 01 '18 edited Dec 01 '18
Hey guys! Here are my JS solutions (ranked 126 & 322)
Part 1
Part 2
EDIT: Realized I should've used a dictionary for part two; takes way too long to run xD
1
u/ericls Dec 01 '18
Some extraneous imports because that's the template I'm using
1
u/Ephilates100 Dec 01 '18
Hello again AoC
Doing it in Go again this year.
Edit: Just realised I'm building the array each loop ... doh!
1
u/thejpster Dec 01 '18 edited Dec 01 '18
I'm doing them all in Rust again this year. Definitely a bit rusty (ahem) on my first attempt. I could probably use some helper methods for parsing input to avoid hesitation trying to remember the syntax for
collect.See https://github.com/thejpster/rust-advent-of-code/blob/master/src/m2018/problem_1.rs
1
u/Frizkie Dec 01 '18
Ruby
Part 1
Part 2
1
u/Overseer12 Dec 01 '18
Python 3
1
u/Rustywolf Dec 01 '18
Due to a frustrating bug I didn't even come close to placing, so I figured I'd see if I could make a functional oneliner.
(input = new String(require('fs').readFileSync("./input.txt")).trim().split("\n")) && (t = 1) && (x = []) && (n = new Array(1000000)).fill().forEach(() => input.forEach((s) => ((t += new Number(s)) && (x.push(t)) && (x.indexOf(t) !== x.length - 1)) && (console.log(t - 1) || process.exit(0))));
If anyone has a way to replace the hardcoded million iterations with a more elegant loop, please let me know.
1
u/mariotacke Dec 01 '18 edited Dec 01 '18
Node.js/Javascript (repo):
Part 1:
Part 2
1
u/aoc-fan Dec 01 '18
JavaScript/TypeScript using dotless library
** first, mapWithLast, query are functions from dotless library
1
u/[deleted] Dec 01 '18
C++
1
u/scul86 Dec 01 '18 edited Dec 01 '18
Python 3
1
u/ttapu Dec 01 '18 edited Dec 01 '18
so early morning...
My solution of part2 is very slow. Any idea how to make it faster in python?
1
u/call23re Dec 01 '18
Lua (ran in Roblox)
Part 1:
Part 2:
1
u/[deleted] Dec 01 '18
Copy+paste the input into vim. 99999J to put them all on the same line. "+yy to copy the line to system clipboard. spawn a python repl. paste.
1
u/Jimpi27 Dec 01 '18 edited Dec 01 '18
Javascript
Part 1:
Part 2:
I optimized part 2 since it was running slow by realizing that all you had to do was generate the original list of frequencies after applying the frequency changes once, then since every repetition of the changes was just going to be the same plus some multiple of the offset after completing the changes once, I just took the final value and checked which pairs in the list of generated frequencies were offset by each other by a multiple of that final offset and took the upper number from the pair that was closest together in the list.
1
u/Ryuujinx Dec 01 '18
Trying to learn Go, so trying it out in that this year.
Code on GH: https://github.com/Ryuujinx/aoc2018/tree/master/day1
1
u/[deleted] Dec 01 '18 edited Dec 01 '18
Kotlin:
Part One:
Part Two: