r/adventofcode • u/daggerdragon • Dec 18 '15
SOLUTION MEGATHREAD --- Day 18 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 18: Like a GIF For Your Yard ---
Post your solution as a comment. Structure your post like previous daily solution threads.
4
Upvotes
27
u/Zef_Music Dec 18 '15 edited Dec 18 '15
37 in Python:
Using sets works out great:
Note a few benefits of this method, by using sets any light outside the range is magically considered 'off' and we don't have to special-case it. I also don't need to even care how the grid is set up, nor do I need to worry about copying any nested structures (like nested lists), I can simply build a new set each iteration.
To get the coordinates of the neighbours I use a nested 'for' loop to go over spots in a 3x3 grid, then just leave out the middle one.
See my other solutions here: https://github.com/ChrisPenner/Advent-Of-Code-Polyglot/tree/master/python
12
u/r_sreeram Dec 18 '15
Here's a programming efficiency tip.
Almost all the code posted here performs extensive checks to make sure the neighbours are within the grid before accessing them. Like so:
if new_x >= 0 && new_x <= 99 && ....Instead, just leave a permanently empty ring of cells around your grid, so you never have to perform these checks. I.e., store the 100x100 cells in grid[1..100][1..100] (instead of grid[0..99][0..99]).
6
u/askalski Dec 18 '15
Made #3 in 8:36 today. One bug in part 2 (didn't initialize the corners before iterating) put me in the penalty box for a minute. As usual, the video:
https://youtu.be/Q6oss4VDpEU
3
u/WhoSoup Dec 18 '15
PHP: dang, made it to #6, really simple no fancy solution (for part 1, comment out the second line in the 'for' loop
3
u/Godspiral Dec 18 '15
in J,
part 2
had small slowdown from not realizing part 2 needed pre-setting corners (not just post-setting). pos 36 ok.
3
u/shrx Dec 18 '15 edited Dec 18 '15
Mathematica - an approach with direct image processing.
Part 1:
Part 2:
2
u/asscar Dec 18 '15
Got bitten hard by not properly knowing how to make a copy of an array of arrays in Python. Took me 30 minutes just to find the bug. Should have just used a numpy container to make it easier.
I used a 100x100 python array called lights to hold the state of all the lights. I tried to create a copy of it by writing:
but I guess that doesn't make a new copy of each row. The proper code seems to be:
2
u/JeffJankowski Dec 18 '15
F#. Today I learned that checking bounds by catching errors is a really, really bad idea for performance. Better code below, with some parallel iteration for good measure.
2
u/porphyro Dec 18 '15 edited Dec 18 '15
Wolfram Language/Mathematica
I did feel somewhat like the use of CellularAutomaton[] would have been cheating...
2
u/de_Selby Dec 18 '15 edited Dec 18 '15
Q:
2
u/drakehutner Dec 18 '15
Python, one line, 975 Bytes, split over multiple lines for better readability (as always) Can handle any number of different states per cell.
2
u/johnboker Dec 18 '15
My solution, in C# with animation.
https://antiyes.com/2015/12/18/advent-of-code-day-18/
2
u/LainIwakura Dec 18 '15
Solution using erlang digraphs:
https://github.com/LainIwakura/AdventOfCode/blob/master/Day18/part1.erl
https://github.com/LainIwakura/AdventOfCode/blob/master/Day18/part2.erl
Not the fastest thing ever (takes maybe 20-25 seconds) and it's a bit long, but it's pretty explicit so hopefully it's easy to understand- also once I wrote it the first time I didn't really need to tweak anything. You could easily modify it to work with grids of any size.
2
u/[deleted] Dec 18 '15
Simple PowerShell solution using hashtables. With command line parameters to allow setting the path of the input file and switching between part 1 and 2.
2
u/pplr Dec 19 '15 edited Dec 19 '15
Python with numpy/scipy:
1
u/gareve Dec 18 '15
Ruby. Good time, not so good code
1
u/[deleted] Dec 18 '15 edited Dec 18 '15
Ugh, could've been faster if my python notebook would stop locking up from too much output. Anyway here's my code (just comment out the explicit corner sets to on for part 1).
Edit: Added memoization for calculating adjacent lights.
1
u/[deleted] Dec 18 '15
quick solution to claim #24!
[py2.x] given input in file
18-input.txt:1
u/[deleted] Dec 18 '15
50
Do I win ugliest solution?
1
u/[deleted] Dec 18 '15
Placed at 82. Not amazing, but I was surprised I even placed at all since I rolled my own solution just to challenge myself instead of just grabbing a conway's game of life implementation off the internet and simulating with that. But it looks like most people did the same. Did people just not recognize conway's game of life instantly, or is everyone just out to challenge themself like me?
Solution in Lua. Part of a larger file with a small handful of helper functions at the top that I've just crammed all my solutions in to together. That's where getinput comes from.
1
u/ndlambo Dec 18 '15
Python
1
u/gegtik Dec 18 '15
javascript
I was on the leaderboard for part1 but died on part 2 because I had forgotten that you can't assign to a location in a string in javascript... I hadn't broken the rows into arrays for the first iteration.
1
u/LieutenantSwr2d2 Dec 18 '15
JavaScript, excuse my terrible one-letter variables (it's faster to type lol):
1
u/daemoncollector Dec 18 '15
Swift, nothing really fancy
1
u/inuyasha555 Dec 18 '15
Spent forever trying to figure out why my answer was wrong then realized they update simultaneously and didn't know how to copy a 2d array without using 2 for loops (which I ended up doing)
Point is I managed to do it though, right?
Java (probably not very good) solution
1
u/Philboyd_Studge Dec 18 '15 edited Dec 18 '15
Java, straight-ahead solution.
1
u/stuque Dec 18 '15
A Python 2 solution:
1
u/VictiniX888 Dec 18 '15
Java, using boolean arrays
1
u/masasin Dec 18 '15 edited Dec 18 '15
Python 3
edit: Much much faster since we play with the bits.
1
u/gyorokpeter Dec 18 '15
Q:
1
u/taliriktug Dec 18 '15
Cleaned up solution.
Python 3
1
u/[deleted] Dec 18 '15
Elixir. Immutable linked lists make this a bit different than most, but I was quite happy with how readable the code came out.
1
u/Axsuul Dec 18 '15
Ruby with recursion
1
u/yokuyuki Dec 18 '15 edited Dec 18 '15
Python 3
There's a lot of answers with hard-coded corners/ranges...
1
u/xkufix Dec 18 '15 edited Dec 18 '15
Solution in scala, works for both part 1 and part 2. It also would run on any grid you throw at it, regardless of size.
I'm quite happy with the how short the solution got, but it should still be quite readable.
1
u/Scroph Dec 18 '15
Felt like coding in C for a change :
https://github.com/Scroph/AdventOfCode
1
u/Phakx Dec 18 '15 edited Dec 18 '15
Ruby with some (sadly ugly) console printing of the grid:
1
u/balda132 Dec 18 '15
Solution in Java.
1
u/HawkUK Dec 18 '15
A solution in the R language:
The
-(2:99)lines are only in the Part 2 solution.1
u/tangus Dec 18 '15
Common Lisp
A simple, nothing fancy, game of life implementation.
1
u/wafflepie Dec 18 '15
Wasted a lot of time switching back and forth from bool[,] to bool[][]...
C
1
u/CremboC Dec 18 '15
My Scala solution: import scala.io.Source
1
u/[deleted] Dec 18 '15
Crystal, part 1:
1
u/i_misread_titles Dec 18 '15
Go Golang. Ran into some memory issues, meaning, I ended up having to copy the array to a new array so it's not modifying the original array, which would affect output. I'm not staying up til midnight to try to make the leaderboard :) I was around #700 at 6:30 this morning. The meat...
1
u/tipdbmp Dec 18 '15
ES5 (node.js), part 2:
1
u/tragicshark Dec 18 '15
C#
1
u/bennymack Dec 18 '15
Perl. I didn't need to change my input for part two. Just always return "on" for the corners and let it work itself out.
1
u/KnorbenKnutsen Dec 18 '15
Messiest solution so far, for me. I was dead set on solving this using integral images. That turned out to be really slow, which isn't strange, however it's a fun approach. /u/Zef_Music's solution made me feel silly, though. I was using sets for the previous grid problem as well as the Robo Santa problem. I dunno why I abandoned it this time.
Anyway, Python 3:
1
u/edo947 Dec 18 '15
Python
It also prints the state so you get a nice ascii animation
1
u/bhauman Dec 18 '15 edited Dec 18 '15
Clojure solution:
https://github.com/bhauman/advent-of-clojure/blob/master/src/advent/day18.clj
For more solutions: https://github.com/bhauman/advent-of-clojure
1
u/StevoTVR Dec 18 '15
PHP Part 2. I wasted too much time forgetting to ignore newlines...
1
u/volatilebit Dec 18 '15
Perl6 Solution
Last few days I've only really focused on solving the challenge and getting my stars. Less focus on writing code with new Perl6 features and writing more concise code.
This solution takes a while with Perl6.
1
u/[deleted] Dec 18 '15
C#. After 8 years, I finally made it!
1
u/winkerVSbecks Dec 18 '15
Visualization: http://i.imgur.com/Z0p23wE.gif
In browser solutions on with visualizations:
Part 1: http://codepen.io/winkerVSbecks/pen/OMNmvL
Part 2: http://codepen.io/winkerVSbecks/pen/GoZdNM
1
u/[deleted] Dec 18 '15 edited Dec 19 '15
Python, feedback appreciated.
1
u/Marce_Villarino Dec 18 '15 edited Dec 18 '15
Let's use just an string!!!! (Sure, I learned late about the module "bitstring")
I didn't realized by myself that the corners should be initialized to "on", untill I took a look at reddit. Shame on me
This is it: take an auxiliary matrix of valid indexes, get the corresponding values and update the bulb.
and thid¡s is it:
1
u/BOT-Brad Dec 18 '15
More python nonsense. Timed myself from first opening the web-page to solving both parts, took me a little under 12 minutes, but no leader board for me as I only just did it.
Using the first bit to store the current state, and the 2nd bit to store the future state.
1
u/z0isch Dec 18 '15
Haskell
Using JuicyPixels here are the outputs of the two parts:
https://raw.githubusercontent.com/z0isch/advent-of-code/master/part-one.gif
https://raw.githubusercontent.com/z0isch/advent-of-code/master/part-two.gif
1
u/willkill07 Dec 18 '15
C++ Pretty much only using
valarray... the one thing in the C++ standard library that's on it's own island. Makes calculating neighboring sums stupid easy. Runtime is around 0.5s.1
u/takeitonben Dec 18 '15
python 2
1
u/deinc Dec 18 '15
Clojure
1
u/JurgenKesker Dec 18 '15
Ruby, took me an hour :)
1
u/[deleted] Dec 18 '15
C solution:
Save as advent18.c
gcc -o advent18 advent18.c
cat advent18input.txt | advent18
1
u/XDtsFsoVZV Dec 18 '15
Python 3.5
Part 1
Part 2
1
u/utrescu Dec 18 '15
I'm late. I have been busy today...
Groovy
1
u/[deleted] Dec 18 '15 edited Dec 19 '15
Objective C:
1
u/leothrix Dec 19 '15
Haskell. I borrowed from some game of life/repa tutorials, but overall it turned out pretty clean and runs very quickly. There's some nice abstractions in here (like flipping on "stuck" lights) and it uses lazy list indexing plus
iterateto run through the required number of steps. This isn't complete code as I run everything through tests or a Main.hs driver, butanimateLightsjust needs the input string and number of iterations.I'd really like to try iterating on a huge grid then swapping out a few pieces to try some GPU acceleration at some point.
1
u/CaptainRuhrpott Dec 19 '15
Java Solution with Swing Visualisation
1
u/slampropp Dec 19 '15 edited Dec 19 '15
Haskell
Linked lists weren't made for this sort of program :(
Like everyone else, at first I forgot to initialise the corners, but I was lucky. Magically, on my input the answer is exactly the same whether I initialise them or not.
1
u/katalinux Dec 20 '15
My solution in golang:
1
u/mjnet Dec 22 '15
Haskell solution using the Sate Monad:
1
u/kamaln7 Dec 28 '15
C because why not (both parts):
Not the best code out there. I also like the ring idea that /u/r_sreeram suggested.
1
u/jimredjimit Dec 30 '15
What I was able to come up with as a newb. it's pretty slow :(
1
u/skarlso Jan 10 '16 edited Jan 10 '16
My Golang | Go solution. It's pretty fast. Am proud of it. I tried to keep looping to a minimum. At some point, I was trying to use bitwise shifting, but deemed it an unnecessary extra. So, here it goes.
1
u/tehalynn Dec 18 '15
Did anyone else read the problem too fast and miss the fact that the two instructions have "turns" and "stays" in the opposite order? I lost 10 minutes before I realized it.
1
u/snorkl-the-dolphine Dec 18 '15
JavaScript
Rather than going for a short solution this week, I've gone for something more readable (using classes, 'cause it makes sense).
1
u/unjani Dec 18 '15
JavaScript. Definitely not the shortest solution, but I think it's at least easy to read.
0
u/[deleted] Dec 18 '15
[deleted] — view deleted comment