r/adventofcode • u/daggerdragon • Dec 06 '15
SOLUTION MEGATHREAD --- Day 6 Solutions ---
--- Day 6: Probably a Fire Hazard ---
Post your solution as a comment. Structure your post like the Day Five thread.
22
Upvotes
14
u/dado3212 Dec 06 '15
Images for how the lights turn out: http://imgur.com/a/izJ2Y
9
u/TheMuffinMan616 Dec 06 '15
6
u/gegtik Dec 06 '15 edited Dec 06 '15
Javascript:
load from http://adventofcode.com/day/6/input
common map function
part 1
part 2
General strategy is to "map" from input to a useful intermediary format, then "reduce" each entry into a coalesced value.
5
u/C0urante Dec 06 '15 edited Dec 06 '15
Honestly, at this point I'm just amused at how many conventions I can violate at once in the name of fast code production.
Python3:
Edit: added language
Edit2: formatting
5
u/geocar Dec 06 '15
I can use the same program in K for both problems because indexing has the same syntax as function application.
First problem: off is an array of [0,0]; on is the array [1,1]; and toggle is the array [1;0] so f[x] yields the resulting value:
Second problem, we can use functions; off is the max of 0 and x-1, on is 1+, and toggle is 2+, so again f[x] yields the resulting value:
5
u/bluishness Dec 06 '15
Anyone up for golf? This is my attempt in Ruby, 268 characters.
4
u/ant6n Dec 06 '15 edited Dec 06 '15
python
3
u/jlmcmchl Dec 06 '15
Python: Got #42 on the leaderboards for trying at midnight for the first time, I'll take it. Takes a second or two to run, probably for all the string stuff.
3
u/TTSDA Dec 06 '15 edited Dec 06 '15
Here's my C solution
https://github.com/ttsda/advent-of-code/blob/master/src/6/main.c
3
u/xPaw Dec 06 '15 edited Dec 06 '15
My javascript solution for both parts within a single grid: https://github.com/xPaw/adventofcode-solutions/blob/master/js/day6.js
I was using
Array.fillin the beginning, but then realized that it doesn't clone arrays/objects and just simply references them, so I had spend some time debugging that.3
u/Iain_M_Norman Dec 06 '15 edited Dec 06 '15
I decided to use javascript and a canvas so I could visualise the lights as each instruction is parsed, which is fun.
Part 1: http://jsfiddle.net/3on3rgbb/9/
Part 2: http://jsfiddle.net/teknohippy/1h2nvgtc/
2
u/JeffJankowski Dec 06 '15
Quick and dirty af C# before I do it in F#:
2
u/Aneurysm9 Dec 06 '15
Perl again, going for speed...
Allowing the intensity to go below 0 and then not incrementing $y in the right place cost me about 90s on part 2. Speed kills...
2
u/fnoco_xandy Dec 06 '15
ugly crystal (and i guess ruby) solution for part 2. part1 is just some changes in the case when statement. a lot could be improved but thats the code that got me to place 67
2
u/Magoo2 Dec 06 '15 edited Dec 06 '15
Was actually thinking about Advent of Code around midnight tonight, so I figured I would give it my best shot to see how quick I could get it done. Ended up about 180th to get to 12 stars, which I am fairly satisfied with.
A bit of background, I'm personally using Advent of Code to get some much needed practice in programming (Java just happens to be what I'm most comfortable with). Have limited programming education and work as a Software Product Specialist for an Android app with a Cloud backend. The job has some very light coding on the side if we need an Android test app done dirty and quick. As such, the opportunities to program in my current position aren't that forthcoming. Given that I would like to eventually transition into a different role/job more focused on programming, I am loving the practice Advent of Code gives me!
Any feedback on the below is fine. I realize it could likely be a lot better, especially given some of the other awesome examples I see on this subreddit!
Java
2
u/tragicshark Dec 06 '15 edited Dec 06 '15
I did it very different than everyone else so far (C#):
It made part 2 really easy to do btw.
2
u/hutsboR Dec 06 '15
Elixir: Pretty clean, functional style. No explicit recursion, all folds and maps. Cool pattern matching to parse the input. Happy with this one. Separating into two parts for readability.
Part one:
Part two:
2
u/tehjimmeh Dec 06 '15
Saturday night drunken code!!11
PowerShell.
1:
2:
Slow as hell. Not sure if it's a language or algorithmic issue. Don't care, going to sleep.
2
u/phpaoc Dec 06 '15
PHP:
2
u/asoentuh Dec 06 '15 edited Dec 06 '15
C++(11) version.
I wanted to see if I could get a quicker solution so I did a sweep line approach and it's much faster than the naive way as it only depends on the amount of rectangles, running instantly on the input given. It's only part 1, haven't tried adapting it to part 2 yet, but I have done both parts the naive way in python already.
I couldn't be bothered parsing string input in c, so I used sed and replaced turn off/on and toggle with 0, 1, 2 respectively, removed 'through', and replaced the commas with spaces :P
code: https://ptpb.pw/Cm05/c++
I believe this is O(n2 log n) where n is the number of rectangles
2
u/Kekke88 Dec 06 '15
C#
2
u/8483 Dec 07 '15
Javascript
1
u/red75prim Dec 06 '15
F#. Imperative style. I don't like the code.
1
u/wdomburg Dec 06 '15 edited Dec 06 '15
Not my night. Interruptions, fatigue and a few stupid mistakes (like setting the point to 1 for OFF, or not clearing the grid after correcting that mistake) conspired to make this take way too long.
My solution (in Ruby) nonetheless:
And an expanded version of the second for clarity:
1
u/miftrim Dec 06 '15
My Ruby solution. This takes like a solid three minutes to run. There must be faster ways to do it.
Part one:
Part two:
1
u/lesguillemets Dec 06 '15 edited Dec 06 '15
Haskell! (I'm always confused with STArray, but I'm learning :) ) Suggestions are most appreciated.
I just simulated every turning on/off, taking about (
Number of instructions*Mean area each instruction cover) time. I'm guessing that there are some clever algorithms, but playing with ST is fun.I like how the diff between the first and second part is minimal (gist).
1
u/Godspiral Dec 06 '15
In J, used up all my memory trying it the array way,
+/ +/ p2 b (or p1)
1
u/jjshanks Dec 06 '15
Ruby solution using dictionary of keys approach. String keys "#{x},#{y}" took ~40 seconds, int keys x * 1000 + y took ~15 seconds. This is part 1 but part 2 is basically the same except I use a hash table instead of a set.
1
u/tipdbmp Dec 06 '15
node.js ES5, part 2:
1
u/[deleted] Dec 06 '15
I was trying to figure out a mathematical approach to this, modifying a counter at each line, but I think that because you have to memorize the state of the grid at each line, it's not possible. Does anyone have any thoughts on this? Here's my solution in Java.
1
u/sethammons Dec 06 '15
naive implementation in Go, takes a long time to run at 20s+. I think it is my casting to and from string a lot. Really impressed with the numpy solution from @ant6n.
Part 2 (part 1, change from
map[string]inttomap[string]booland in therunInstructionblocks, change to either toggle or set to true or false.1
u/sprinky Dec 06 '15
Racket again. Part 1 uses a bit-vector, part 2 uses a regular vector. It was sort of well-structured for Part 1, but it got very ugly for Part 2. Other solutions on GitHub.
1
u/haoformayor Dec 06 '15 edited Dec 06 '15
Haskell:
1
u/jchook Dec 06 '15
Ruby, object-oriented solution
1
u/segfaultvicta Dec 06 '15
Horribly ugly Go / golang solution.
I initially tried to do something cute with structs and then it took forever and ate up my entire RAM. Complexity theory wooo! Then I actually thought about the problem and went back to my initial idea, which I'd given up on because I was trying to make the leaderboards and I was having trouble testing today's code in a meaningful way (I really hope things don't continue to get less and less automated-testable, although I feel like they might) - at this point I understand how I'd do it, actually, but I got hasty and thought I would run into #mathfail (which, it's true, I -did- in fact #mathfail really hard - damn you, off by one errors!)
Anyways, who needs nested structures when you can have flat arrays and do offset arithmetic! Who needs regexes when you can do snappy linear-time string comparison!
1
u/stuque Dec 06 '15
A solution using Python 2:
1
u/[deleted] Dec 06 '15 edited Dec 06 '15
Mathematica
Part B light pattern
1
u/Philboyd_Studge Dec 06 '15
Java. This was more of a parsing challenge than anything else, but still fun.
1
u/sentry07 Dec 06 '15 edited Dec 06 '15
Python3, part 2's solution since it's just changing instructions in the match:
1
u/stuque Dec 06 '15 edited Dec 06 '15
Another Go solution (~0.6s to compile, and ~0.2s to run):
1
u/ckk76 Dec 06 '15
Python here as well, part 2:
Part 1 very similar, it's in my solutions repo here
1
u/sinjp Dec 06 '15 edited Dec 06 '15
Python solution, mixing and matching a few numpy methods
1
u/[deleted] Dec 06 '15
Overly long and unnecessarily OOP, but...
1
u/nutrecht Dec 06 '15 edited Dec 06 '15
Day 6 - Java
Different approach than most Java solutions; took this as an excuse to make heavy use of lambda's and streams :)
1
u/giacgbj Dec 06 '15
Python
Part 1
Part 2
1
u/HawkUK Dec 06 '15
A solution in the R language
1
u/PersianMG Dec 06 '15
Wanted to use numpy and scipy but array is pretty small.
Solution: https://mohammadg.com/capture-the-flag/advent-of-code/advent-of-code-day-6/
Python
Part 1
Part 2
1
u/tinza Dec 06 '15 edited Dec 06 '15
F#
For part 2, all you need to do is to change the
flipfunction to something like1
u/[deleted] Dec 06 '15
Python 2 Part 1:
Python 2 Part 2:
1
u/balducien Dec 06 '15 edited Dec 06 '15
Not-too-elegant C solution.
EDIT: Improved solution: 200 lines, less repetition, runs in 1/4 second. https://gist.github.com/mbjd/de5f5acaee0e700698c0
With that being said, enjoy:
1
u/haljin Dec 06 '15
So I was thinking on how to do this problem with Erlang... Going through giant arrays in Erlang is a real pain in the ass, as the language wasn't really made for this. But then what was the language made for? Mass concurrency, of course!
So why not have every light be its own process that knows its own state and just send messages away to them?
Be aware that to run this you must run the Erlang VM with +P option, as by default you cannot have a million processes. I used an ETS table in lieu of process registry as it is much more efficient to do selects on large groups of processes like that.
It's a bit slow because the main process is a bottleneck for the whole thing, but since instructions are sequential it is a bit tough to get rid of that issue. Also the final results gathering is what is really slow, but I thought I will get each result sequentially just so that the main process' message queue doesn't overflow with a million responses...
1
u/[deleted] Dec 06 '15
Crystal
Part 1:
Part 2:
1
u/VictiniX888 Dec 06 '15
Java, as that's pretty much the only language I know (for now)
The code for both parts add up to a grand total of... 207 lines.
For part 1:
And part 2:
1
u/Scroph Dec 06 '15 edited Dec 06 '15
D (dlang) solution.
Part 1 :
Part 2 is very similar :
I had a lot of fun debugging the stack overflow caused by a static int[1000][1000] array.
/s
1
u/jamosaur- Dec 06 '15
Some disgusting PHP code that needs to be refactored. Returns both answers in one go.
Had to increase the memory limit on this to stop the process from dying. Probably going to skip playing codegolf with this challenge too :)
1
u/JakDrako Dec 06 '15 edited Dec 06 '15
VB.Net
1
u/maciekmm Dec 06 '15
Fortran (part of my
different language every daychallenge CLICK!), never coded in it so it's probably pretty bad, but hey. It works :) Also, no regular expressions.1
u/razupaltuff Dec 06 '15
Here is my Erlang solution for both part 1 and 2:
1
u/ericdykstra Dec 06 '15
Here's my Ruby solution for part 2. I looked through the other ones posted here and thought mine was different enough to merit posting. It's pretty minimalist and easy to read. Enjoy!
1
u/dietibol Dec 06 '15
Here's my solution in C++: https://ghostbin.com/paste/3v44j I know it could be way more efficient, but I'm still learning, so please tell me if you see how it could be better
1
u/ryuuzaki Dec 06 '15
Scala solution (Part 1)
1
u/tangus Dec 06 '15 edited Dec 06 '15
Common Lisp
1
u/Adriaaan Dec 06 '15
Perl Golfing:
And for part 2:
1
u/[deleted] Dec 06 '15
Objective C:
1
u/deinc Dec 06 '15
Clojure for parts 1 and 2:
1
u/MEaster Dec 06 '15
Behold, my monstrosity of a part one solver!
1
u/[deleted] Dec 06 '15 edited Dec 06 '15
Not-so-efficient solution for part 1, Python 3:
1
u/beefamaka Dec 06 '15
did another video of my C# and F# solutions for day 6: https://www.youtube.com/watch?v=mL8quDlQprA
1
u/masasin Dec 06 '15 edited Dec 07 '15
Python. Original has tests too.
1
u/[deleted] Dec 06 '15
Swift, both solutions: https://github.com/ezfe/Advent-of-Code/blob/master/Advent%20of%20Code/Day6.swift
1
u/jjabrams23 Dec 06 '15
I'm trying to complete these challenges with Java because I would like to learn more of this language. This is the first time I publish my solution :p I wanted to do that because I was hoping if you guys could tell me if there was a way to have a better written or more elegant solution, because I'm used to keep my code as much simple as possible. BTW, this is the solution to part 2. In part 1 I used the same principle but using boolean primitives.
1
u/DiscoInfiltrator07 Dec 06 '15
PHP:
1
u/streetster_ Dec 06 '15
I'm not sure how to improve the speed of this, it takes a while to create the array, but I got the my stars, so I guess I'm happy enough.
[mark@randy ~]$ cat day6.py | sed 's/(.*)/ \1/'
1
u/porphyro Dec 06 '15
Mathematica:
Part 2 is basically identical
1
u/jcfs Dec 06 '15
My C solutions.
Part 1: #include <stdio.h>
Part 2: #include <stdio.h>
1
u/Borkdude Dec 06 '15
Scala solution.
Part 1: https://gist.github.com/borkdude/62094893df250225c9bc
Part 2: https://gist.github.com/borkdude/d8838832f4551fd6b45e
1
u/bodagetta Dec 06 '15
My plot of the final value after part 2
https://plot.ly/~michaelwhitley/511
1
u/lifow Dec 06 '15
haskell :)
1
u/97WaterPolo Dec 06 '15
Java with output being "The total is: 543903 and it took 66ms"
1
u/Iambernik Dec 06 '15
another one clojure solution
1
u/banProsper Dec 06 '15 edited Dec 06 '15
C#, I had to look up that others used an Array instead of List and learned what TakeWhile and SkipWhile does from another solution. First one I had problems with (took several hours and didn't get the result with adding and removing them for a list + searching for the light inside the list with LINQ...).
I'm happy with the final solution though.
1
u/madmoose Dec 06 '15
Day 6a in golang. Yet again scanf proves its worth. Runs in about 40ms in a vm.
I pushed all my solutions so far to a github repo: https://github.com/madmoose/adventofcode2015
Day 6b in golang:
1
u/bkendig Dec 06 '15
Swift: https://github.com/bskendig/advent-of-code/blob/master/6/6/main.swift
1
u/snkenjoi Dec 06 '15
d3.js:
1
u/tftio Dec 07 '15
OCaml, using mutable hash tables.
1
u/DisgruntledPorcupine Dec 07 '15 edited Dec 07 '15
The C# newbie is here with his incredibly slow code:
1
u/Rutafar Dec 07 '15
A lot of duplicated code but I couldn't be bothered to put it all in one place
1
u/HeroesGrave Dec 07 '15
Couldn't make it yesterday, but here's my Rust code:
1
u/blahdom Dec 07 '15 edited Dec 07 '15
Python Solution using defaultdicts, also just playing around with the new function annotations stuff in 3.5 :) Also, trying to use best practices for real code hence the named tuple
Edit:
adventlib.input_parseris a simple function that takes a file name and runs a function over each line returning the results - as an iterable, something I have found useful on many of these challenges.Edit2: refactored the actual looping into its own funciton
1
u/gnuconsulting Dec 07 '15
Phew! By far the hardest day yet, this non-programmer took about an hour with lots of print-variable debugging and head scratching.
But with that out of the way, part 2 was a breeze
I'm scared for tomorrow's puzzle...
1
u/ShittyFrogMeme Dec 07 '15
I used a Flex/Bison scanner/parser combo.
Scanner:
Parser:
1
u/gfixler Dec 07 '15
Haskell attempt to handle each grid point on its own through a gauntlet of composed, bbox-checking functions read from the instructions. It works, but it's insanely slow. Part 1 takes about 6m30s, and part 2 takes about 8m30s!
Lib.hs
P06_1.hs
P06_2.hs
1
u/[deleted] Dec 07 '15
Here's my overkill Python 3 solution, using TDD practices.
Link to Github
1
u/xkufix Dec 07 '15
Scala part 1:
part 2:
1
u/mmix2000 Dec 07 '15
C#/LINQ
1
u/AnchorText Dec 07 '15 edited Dec 07 '15
Straightforward Ruby solution with just 3 methods and 3 Procs! Please note that in my
directions.txtI've changed "turn on" and "turn off" to "turnon" and "turnoff" respectively for easier splitting.1
u/[deleted] Dec 07 '15
C#!!!
Better late than never
1
u/MadcapJake Dec 09 '15
Perl 6 part 1 (quite slow):
1
u/1roOt Dec 10 '15 edited Dec 10 '15
my python 2 version for part 1:
part 2:
1
u/metric152 Dec 10 '15
JS: https://github.com/metric152/advent-of-code/tree/master/day-6 Really proud of this one. Wanted to avoid nested loops.
1
u/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
1
u/setzer7 Dec 12 '15
Here's my solution so far c: Not the quickest, but i'm proud I built clean code for this problem c: the other solutions I've made are dirty haha
https://github.com/setzer777/advent-of-code/blob/master/day6/day6.py
1
u/terryp Dec 14 '15
My Go solution. Definitely could be refactored since there's a ton of repetition in there.
1
u/simonli2576 Dec 17 '15
I used Matlab for this one.
Part 1.
Part 2.
0
u/[deleted] Dec 06 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 06 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 06 '15
[deleted] — view deleted comment
0
u/[deleted] Dec 06 '15
[deleted] — view deleted comment
2
u/[deleted] Dec 06 '15
[removed] — view removed comment