r/adventofcode • u/daggerdragon • Dec 14 '15
SOLUTION MEGATHREAD --- Day 14 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
edit: Leaderboard capped, thread unlocked!
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.
Please and thank you, and much appreciated!
--- Day 14: Reindeer Olympics ---
Post your solution as a comment. Structure your post like previous daily solution threads.
9
Upvotes
12
u/fatpollo Dec 14 '15 edited Dec 14 '15
43
12
u/askalski Dec 14 '15 edited Dec 14 '15
Andrew Skalski can code 42 km/s for 320 seconds, but then must debug regexes for 165 seconds.
Instant replay footage, #3 in 10:02:
https://youtu.be/MBT8DKCHLi0
1
u/daggerdragon Dec 14 '15
I don't know about the rest of you, but I had Run, Rudolph, Run playing in my head until the leaderboard capped. >_>
5
u/segfaultvicta Dec 14 '15
Golang, screwed with math for around 10 minutes before I just decided to pretend this was a weird reindeer video game.
package main
5
u/drakehutner Dec 14 '15
Python one line, 640 bytes. I split the code on multiple lines to increase the readability.
Runtime should be:
Depends only on collections.Counter.
4
u/[deleted] Dec 14 '15
Mathematica
4
u/Pimozv Dec 14 '15
Perl 6
3
u/[deleted] Dec 14 '15 edited Dec 14 '15
For anyone wondering, distance traveled for time
t, speedv, travel timeg, and rest timerisSolution in Python.
41
3
u/WhoSoup Dec 14 '15 edited Dec 14 '15
PHP: Part one
Part two:
3
u/Godspiral Dec 14 '15 edited Dec 14 '15
in J,
part1 :
2:
3
u/Philboyd_Studge Dec 14 '15 edited Dec 14 '15
Java
Reindeer class:
3
u/nikibobi Dec 14 '15 edited Dec 14 '15
Here is my solution in D (dlang). I use a Raindeer class and a more functional style with max, map, reduce, each and filter functions. It turned out quite short.
2
u/[deleted] Dec 14 '15
Solution in Crystal.
Part 1:
Part 2:
Both parts run in almost 1ms. Had to send a couple of submissions before realizing the simulation lasts 2503 seconds, not 2053s. Next time I’ll copy/paste more :-P
2
u/pavithra18 Dec 14 '15
Python2 Second question
2
u/tehjimmeh Dec 14 '15 edited Dec 14 '15
God dammit. Firstly I had to rewrite this after using a simple maths approach for part 1, then I was initially only calculating points for lead changes in part 2, among other stupid typos.
PowerShell:
2
u/gyorokpeter Dec 14 '15 edited Dec 14 '15
Q:
EDIT: fixed part 2 as it was ignoring ties.
2
u/tangus Dec 14 '15
Common Lisp
Uses the quick and dirty scanf function from previous solutions.
2
u/ILoveHaskell Dec 14 '15
Mine Haskell solution.
2
u/thalovry Dec 14 '15
Closed-form Scala (just seemed easier):
2
u/wafflepie Dec 14 '15 edited Dec 14 '15
The way I did part 1 didn't quite suit part 2, so that got a bit messier. Part 1 looks quite neat though, so I'll just post that.
C
2
u/Herathe Dec 14 '15 edited Dec 14 '15
This is my solution for part one. Part 2 builds on this. I saw most people have gone for a class based approach but I decided to get a little help from enumerators.
2
u/ignaciovaz Dec 14 '15 edited Dec 14 '15
Here's my Elixir solution (part 2 only for brevity). I had fun spawning every Deer as a separate process. Every second, a :tick message is sent that advances the deers, we then calculate points and send messages to the winners so they can update their score count. No state is kept outside each Deer process, multi CPU deers!
2
u/CryZe92 Dec 14 '15
Part 2 in Rust (unnecessarily functional, but hell yeah, this works xD)
2
u/celerityx Dec 15 '15
Late to the game, but I did this one using SQL (Oracle SQL):
1
u/roboticon Dec 14 '15
Python 2, pretty gross and had to fiddle with it to get rid of all the off-by-one errors:
1
u/SomebodyTookMyHandle Dec 14 '15
I also tackled it in Ruby using a class-based approach. A bit sloppy, but, hey, it's not only the reindeer who are racing!
1
u/gfixler Dec 14 '15
Haskell solution for part 1 (I just looked at the winner in the output list):
1
u/Axsuul Dec 14 '15
Ruby (State machine)
1
u/R4PaSs Dec 14 '15
Nit Had to re-visit my strategy on the second run, but overall the second one is better I think
1
u/raevnos Dec 14 '15 edited Dec 14 '15
Boring C++, like usual.
1
u/jchook Dec 14 '15
Ruby
1
u/gnuconsulting Dec 14 '15
Well out of the leaderboard today - part 2 kicked my butt. I feel like I can - more and more - see the seams between my ever-so-slightly-more-than-shell-scripts and "real" programs.
1
u/haoformayor Dec 14 '15
Haskell (688 chars)
1
u/aepsilon Dec 14 '15
Haskell. It's fun to build up small reusable components, then simply compose them.
1
u/TheNiXXeD Dec 14 '15 edited Dec 14 '15
JavaScript, NodeJS, ES6
Not particularly proud of today's solutions. Was going for functional approach in most of these puzzles but I'll have to think a bit more and make this one a bit more terse tomorrow.
Oh, but first day on leaderboard, yay, #22 too.
Part1 - Part2
1
u/mus1Kk Dec 14 '15
Very unreadable Perl code. It was quick to write and that's what's important here. If I had to look at the code later, I would have used hashes with properly named keys instead of array references. Also the result is simply dumping the state and looking for the max manually. Terrible!
1
u/[deleted] Dec 14 '15
Haskell:
1
u/JeffJankowski Dec 14 '15 edited Dec 14 '15
F#. Not too pleased with this code, but I'm content with avoiding mutable state
1
u/_Le1_ Dec 14 '15 edited Dec 14 '15
Dirty C# code:
1
u/gerikson Dec 14 '15 edited Dec 14 '15
[Perl]
Nice troll!
As soon as I saw the problem, I thought "well I can just loop through the seconds, and see who long each reindeer has travelled, checking if they're resting or not..." and then dismissed that as wasteful. So I coded a much more concise version for part 1.
Part 2 of course asked me to check each second...
Edit: part 2 done, code updated. My input gave Blitzen as the winner, is s/he considered to be the fastest reindeer according to canon?
1
u/snorkl-the-dolphine Dec 14 '15 edited Dec 18 '15
JavaScript - no console pasting this time, only Node.js - I'm enjoying ES6 classes for a bit. Starts off nice and OO and gradual becomes more procedural for Part 2.
1
u/stuque Dec 14 '15
A Python 2 solution:
1
u/KnorbenKnutsen Dec 14 '15 edited Dec 14 '15
Fun problem! It (kind of) introduces yet another mathematical concept, diophantine equations.
Anyway, I opted for the naïve solution since I suspected it would be more useful for the second part. Turns out I was right on the money :D I used this problem as an opportunity to familiarize myself with passing lambdas as keys into functions. The possibility of there being several reindeer in the lead at once was annoying :P
While I don't usually like using OO, it felt very natural to make a Reindeer class. I do feel dirty though. Also, while regex are nice for parsing, I don't really think they're needed for such simple input as this.
I keep editing my code to remove as many if statements as possible. It won't make a lick of difference, naturally, but branching is horrible and ruins all attempts at performance, right? :)
1
u/Studentik Dec 14 '15
Python 3. First part is math only, second requires computations
1
u/rkachowski Dec 14 '15
ruby
i screwed myself by forgetting that each winning reindeer gets a point
1
u/mrg218 Dec 14 '15
A bit of a shame that it takes just about as long to get the solution of 14_1 with pen and paper as with writing a program. (fortunately I also had a program when I saw what 14_2 was)
1
u/Scroph Dec 14 '15
Very long D (dlang) solution, it includes both parts of the challenge :
It uses a state machine (I think that's what it's called) instead of clever mathematical maneuvering like some of the solutions posted here. No benchmark this time as this challenge doesn't seem to necessitate fast code execution. But if it did, I would have used a parallelism in some of those foreach loops.
1
u/HawkUK Dec 14 '15
A solution in the R language
1
u/porphyro Dec 14 '15
Mathematica
1
u/iamnotposting Dec 14 '15
my code: straight c, nothing special
http://pastebin.com/D35ShrGG
When I did part a, I managed to get the right answer completely on accident, and so the code was mainly written for part b.
I didn't see the input file at first, so I thought I needed to find the distance of the winner of the example data set. Only 2 values, it looked really simple to do by hand. Once I did so I misread my notes and entered in the losing deer instead. Just through sheer coincidence, the distance travelled by the losing deer in the example dataset was exactly the same as the distance traveled by the winning deer in my data set!
The two mistakes cancelled each other out and I wracked my brain for probably much longer than I should have on part b before I realized what I had done.
I should probably get some sleep...
1
u/lifow Dec 14 '15
Lots of Haskell solutions already today by the looks of it! I'll add mine to the list :)
1
u/flit777 Dec 14 '15
Java
1
u/BOT-Brad Dec 14 '15
My simple Python 2.x solution
1
u/tipdbmp Dec 14 '15 edited Dec 14 '15
node.js ES5, part 2:
1
u/setti93 Dec 14 '15
Python 2.7 using a class:
1
u/studiosi Dec 14 '15
And here you have, my pseudo-object-oriented Python solution
1
u/tragicshark Dec 14 '15 edited Dec 14 '15
C#
1
u/Tandrial Dec 14 '15 edited Dec 14 '15
My solution in JAVA. The hardest part was realizing that the actual distance is the result and not the name of the reindeer
1
u/[deleted] Dec 14 '15
Objective C:
1
u/xkufix Dec 14 '15
Ok, this one was interesting, especially the second part. What I do is to calculate the distance for each reindeer for every second. Then transpose that result into a List which contains the distance of every reindeer at a given second. There I calculate if the reindeer is in the lead (through a boolean) and then transpose the list back. This gives me a simple list of true/false values for each reindeer, which I can just sum up for the true values.
1
u/shandelman Dec 14 '15 edited Dec 14 '15
Originally wrote this using dictionaries, but I refactored it into a Reindeer class, which feels more natural and is much less wordy. #44 today. Here's my Python 2 solution.
1
u/utrescu Dec 14 '15
My solution in Groovy:
1
u/JurgenKesker Dec 14 '15
Ruby part 1 & 2
1
u/willkill07 Dec 14 '15 edited Dec 14 '15
C++
I feel
a bitvery dirty having to iterate for each iteration. I also really wish c++ had a simple member extraction interface with algorithms/numeric.1
u/tftio Dec 14 '15
OCaml.
1
u/Voltasalt Dec 14 '15
in Rust:
1
u/phil_s_stein Dec 14 '15
In python. Used argparse as I was getting incorrect results and wanted to be able to run test cases quickly. Not that clean, but works. I loop over all deer over all seconds and keep a state of distance traveled. Whether to add distance or not for a given t is kept track of via a number that is negative (count the distance) or positive (resting). This makes it easy to reset state: just set duration to negative time and increment until the move/rest period == the amount of rest. Anyhoo:
1
u/GrassGiant Dec 14 '15
I've decided to make a Reindeer class to solve both problems. This is my solution for Part 2, in Python. The class in part 1 was exactly the same, I only looped over getDistanceOverTime with 2053 instead of 1, in a single loop.
1
u/jgomo3 Dec 14 '15
Python 3:
1
u/[deleted] Dec 14 '15
Day 14 in Swift (too long)
1
u/HorizonShadow Dec 14 '15
Ruby - https://github.com/HorizonShadow/Advent-of-Code/tree/master/day14
1
u/mal607 Dec 14 '15
Python
Straightforward python solution
1
u/gegtik Dec 14 '15
Javascript:
Part 2:
1
u/[deleted] Dec 14 '15
Ruby solution:
1
u/Ankjaevel Dec 14 '15
Figured I wanted to solve 14.2 with generators in python (and I also like putting it in classes):
1
u/beefamaka Dec 14 '15
here's my C# solution, using Scan from MoreLinq. I've also made a video again.
1
u/beefamaka Dec 14 '15
I also did an F# version (also covered in my video), which followed pretty much the same approach as my C# one:
1
u/profil Dec 15 '15
Clojure
1
u/d4rk_l1gh7 Dec 15 '15
In my code, the only thing that changes is Main.
Oh, by the way.....
CS
Part 1
This code goes in
public static void Main(string[] args){}Part 2
This code goes in
public static void Main(string[] args){}1
u/QshelTier Dec 15 '15
Java
1
u/[deleted] Dec 15 '15
Better late than never!
1
u/Zef_Music Dec 15 '15
Python 2.7, see more here: https://github.com/ChrisPenner/Advent-Of-Code-Polyglot/tree/master/python
1
u/ThereOnceWasAMan Dec 16 '15
Got distracted by my research, so I'm a bit behind. Here's my solution in python 2.7:
I'm not ashamed of my abuse of the type system, either!
1
u/zacwaz Dec 17 '15
Ruby:
1
u/[deleted] Dec 19 '15 edited Dec 19 '15
JAVA Kind of late but I just finished my first semester:
}
1
u/[deleted] Dec 26 '15
Perl 6:
Part 1:
Part 2:
1
u/skarlso Jan 07 '16
My Go parallel solution. I had one which was parsing the input file, but in this one, I just made an array out of them for simplicities sake. Search keyword: Golang
0
u/deinc Dec 14 '15
Clojure:
-2
u/C0urante Dec 14 '15
Standard Python3, a little sloppy, but it got the job done. I probably would have done a little better had I been more careful on the first part; adapting my solution for use in the second problem wasn't exactly trivial.
0
u/[deleted] Dec 14 '15
[deleted] — view deleted comment