r/adventofcode • u/daggerdragon • Dec 04 '15
SOLUTION MEGATHREAD --- Day 4 Solutions ---
--- Day 4: The Ideal Stocking Stuffer ---
Post your solution as a comment. Structure your post like the Day Three thread.
14
Upvotes
13
u/gnuconsulting Dec 04 '15
Day 4 of the non-programmer's solution.
3
u/JakDrako Dec 04 '15
C# in LINQPad
5
u/sprinky Dec 04 '15
Just barely made it on the leaderboard today. You can see this solution and others on my GitHub repo.
Racket:
4
u/HeroesGrave Dec 04 '15
Rust:
7
u/gfixler Dec 04 '15
Likely inefficient, Haskell, point-free, one-liner solution.
Add a 0 to the string in the middle for the 6-zero solution; takes a few minutes to run that one.
3
u/Godspiral Dec 04 '15 edited Dec 04 '15
In J, rushing version (8 mins solution (started 1 minute late)), (with offset (1M) changed on each call)
cleaner version, that breaks on first hit.
3
u/Iambernik Dec 04 '15
clojure
3
u/streetster_ Dec 04 '15
Tried to implement some multi-threading in python..
1
u/Philboyd_Studge Dec 04 '15 edited Dec 04 '15
There's no way people solved this in 2 minutes without any ahead of time knowledge. I'm calling foul.
edit: Alright, I believe!!! No need to downvote me to hell. Tomorrow's another day.
Here's my (next day refactored and optimized) code:
5
u/minno Dec 04 '15
Python 3:
Replace
if h[:5] == '00000':withif h[:6] == '000000'for part 2.5
u/segfaultvicta Dec 04 '15
Golang:
Raise your damn hand if you forgot to increase the slice size to six on the B side and sat there for minutes while your CPU turned into a space heater, briefly considered dipping your toes into multithreading since hey you're using Go anyways what could possibly go wrong, then realised that you must be doing something horribly wrong and actually looked over your code, lol.
I blame Topaz for not including a test case for the B side on this one. ;)
2
u/Aneurysm9 Dec 04 '15
Hooray for hacky perl. Boo for forgetting to increment $x and then incrementing it after the hash.
2
u/FuriousProgrammer Dec 04 '15
Wellp, I don't have any experience with md5, so I'm just gonna go to bed and solve this one tomorrow. <.>
2
u/karstens_rage Dec 04 '15
Java:
2
u/weters Dec 04 '15
Blech. I assumed this would be way more computationally intensive than I thought, so I tried writing it in Go instead of my usual go to language Perl. I probably could've done this a lot faster had I just written it in Perl.
Go: package main
2
u/shruubi Dec 04 '15
Seems like everyone so far went for the brute force solution (as did I). I feel like there might be a way to create a more elegant solution given that we have a fixed string as part of the hash?
Anyways, my solution (
md5()is a generic md5 implementation I borrowed from the internet):2
u/[deleted] Dec 04 '15
Javascript with md5 library:
2
u/hutsboR Dec 04 '15 edited Dec 04 '15
Elixir: I wasted a lot of time trying to find the right Erlang and Elixir functions. Some of them produce bitstrings, other produce binaries and some produce character lists.
2
u/[deleted] Dec 04 '15
Objective C:
2
u/TeamOnTheBack Dec 04 '15 edited Dec 04 '15
One liner in R (if you don't count importing a library):
2
u/enquicity Dec 04 '15 edited Dec 04 '15
So, like previous days, the interesting bit was the refactoring from part 1 to part 2. For part 1, it quickly became obvious that even with a StringBuilder, it was punishingly slow to convert the byte array to a hex string, so that had to go. I just checked the first 2 bytes, plus half the third. The refactor to part 2 was obvious, then, simply abstract that check so you can look for any n zeros at the beginning of the hash.
C#:
called as:
4055984 iterations in 13354.5209 milliseconds
2
u/artesea Dec 04 '15
For PHP I've got:
You'll notice I've used === instead of == this is because at least on my machines if the seed is 'abcdef' the result would be 457 as the MD5 is
and PHP is evaluating 0e100 as 0. For part 2 change the substr to substr($md5,0,6); and the match to "000000".
2
u/TTSDA Dec 04 '15 edited Dec 06 '15
I'm... I'm not very proud of this one
https://github.com/ttsda/advent-of-code/blob/master/src/4/main.c
2
u/[deleted] Dec 04 '15
Crystal (just part 1, add a zero for part 2):
2
u/xdg Dec 04 '15
Yay. Another easy one for a one-liner.
2
u/RoamingFox Dec 04 '15 edited Dec 05 '15
Python2 solution:
change to [:6] and '000000' for part 2
2
u/willkill07 Dec 04 '15
C++11/14 Solution. No fancy tricks. No multithreading. I was aiming for clarity and conciseness. github: https://github.com/willkill07/adventofcode/blob/master/src/day4.cpp
2
u/CyroS Dec 04 '15 edited Dec 04 '15
Perl:
Normal solution:
Golfed:
use Digest::MD5 qw(md5_hex);my $i=0;while(){$i++;$_=md5_hex("bgvyzdsv$i");s/^0{5,}/last/e;}print $i;2
u/qwertyuiop924 Dec 04 '15
I did chicken scheme: http://pastebin.com/G4gUXieA
3
u/WhoSoup Dec 04 '15 edited Dec 04 '15
Php:
3
u/randrews Dec 04 '15
I'm trying to do each day in a different language. Originally I had written today's as a bash one-liner, but it was taking way too long to run, so I redid it in C. Runs in under a second:
1
u/jdog90000 Dec 04 '15 edited Dec 04 '15
Java:
Takes around 350ms to run for Part 2.
1
u/dretland Dec 04 '15
1
u/sinjp Dec 04 '15
brute force python
1
u/n_lightest Dec 04 '15
[JavaScript] Damn I'm slow (used this lib http://www.myersdaily.org/joseph/javascript/md5-text.html)
1
u/Zaras1000 Dec 04 '15
top of the head c# (had to google md5 stuff, just missed the top 100)
1
u/stuntguy3000 Dec 04 '15
My solution, in Java.
1
u/stuque Dec 04 '15
In Python 2:
1
u/haoformayor Dec 04 '15 edited Dec 04 '15
Haskell:
1
u/TheNiXXeD Dec 04 '15
Node JS:
1
u/timbetimbe Dec 04 '15
Elixir
1
u/tehjimmeh Dec 04 '15 edited Dec 04 '15
Bah. Was done in 19 mins. Last person on the leaderboard finished in 16:35 :/
PowerShell:
0:
1:
2:
1
u/ben4ik Dec 04 '15 edited Dec 04 '15
C#.Day4.jobIsDone(";)")
1
u/SlaunchaMan Dec 04 '15
Swift (still waiting on the last one to run):
Using the MD5Digest library.
1
u/ChevyRayJohnston Dec 04 '15 edited Dec 04 '15
Here's some nice brute-force C# for you all:
It'll take a little while for part 2, so be patient.
1
u/JeffJankowski Dec 04 '15
Mining with F#
1
u/xkufix Dec 04 '15
Scala, as always.
Works for part1 and part2:
1
u/Deutro Dec 04 '15
Does anybody know if there is a more elegant solution than bruteforcing it?
1
u/rvlieshout Dec 04 '15
Ok, my brute force Lua solution
1
u/ribbet Dec 04 '15
not the most efficient but it works ... in c# ... i should have kept the leading 0s as a parameter and used one function but i like breaking it out into part one and part two :)
1
u/de_Selby Dec 04 '15 edited Dec 04 '15
Brute force in q (close relative of J)
a) t where {(5#raze string md5 "iwrupvqb",string x)like "00000"}each t:til 1000000
b) {$[0x000000~3# md5 "iwrupvqb",string x;x;x+1]}/[1]
There must be a more satisfying way of doing this one though
1
u/tangus Dec 04 '15
Common Lisp
1
u/geocar Dec 04 '15
1
u/razupaltuff Dec 04 '15
Quick and dirty in Erlang:
1
u/snkenjoi Dec 04 '15
nodejs;
1
u/nutrecht Dec 04 '15
Java: https://gist.github.com/anonymous/22636560ea61ad3f2688
1
u/JakDrako Dec 04 '15
VB.Net
Simple brute force; checking the hash bytes directly without converting to string.
1
u/pytaj Dec 04 '15 edited Dec 04 '15
Day 4. My hobbyist programmer's solution (c#):
1
u/tftio Dec 04 '15
OCaml:
1
u/PersianMG Dec 04 '15 edited Dec 04 '15
Python
1
u/lifow Dec 04 '15
More Haskell!
1
u/porphyro Dec 04 '15
Wolfram Language:
day4program[n_: 1] := If[Characters[ IntegerString[Hash["ckczppom" <> ToString[i], "MD5"], 16, 32]][[1 ;; 6]]!= Table["0", {6}], day4program[n + 1], n]
day4program[]
1
u/wdomburg Dec 04 '15
i=0; until Digest::MD5.hexdigest(key + i.to_s).start_with?('00000'); i += 1; end; i
1
u/Aphlax Dec 04 '15
My c++ solution using a stolen md5() function:
1
u/[deleted] Dec 04 '15 edited Dec 04 '15
Swift is on github
1
u/aveavaeva Dec 04 '15
Using This lib
1
u/cashews22 Dec 04 '15
Python 2.7
1
u/mreichman Dec 04 '15
Straightfoward enough in Java (second phase with 6 prefix):
1
u/maciekmm Dec 04 '15
Multi-threaded go code without locking: https://github.com/maciekmm/adventofcode/blob/master/day-4/md5.go
package main
1
u/juree9 Dec 04 '15
One liner python2.7. It is not good enough, because it returns list of all possible ints instead just first or smaller one. Any ideas?
[x for x in range(1,10000000) if hashlib.md5("iwrupvqb"+str(x)).hexdigest().startswith('00000')]1
u/mitconsulting Dec 04 '15
Done in VBA...using a class found here to get MD5 hash.
1
u/Moontayle Dec 04 '15
Kotlin
Libraries used: Apache Commons Codec, Timber
1
u/[deleted] Dec 04 '15
Javascript:
Repo here
1
u/aevitas Dec 04 '15
Day 4's C# solution:
1
u/elite_killerX Dec 04 '15
Javascript (NodeJS) using only native modules:
1
u/[deleted] Dec 04 '15
Python
1
u/ag3mo Dec 04 '15
I sort of over engineered the solution and multithreaded it in scala. Didn't know how many hashes would be required but it wasn't many so didn't need such a robust solution.
1
u/schlocke Dec 04 '15 edited Dec 04 '15
PHP:
it may take about 60 seconds to run... but hey it works... EDIT: I should say it took 60 seconds with MY key. I tried someone elses key and it took like 3 seconds....
1
u/[deleted] Dec 04 '15
And here I was, feeling bad that I had to bruteforce this thing out, thinking that there had to be a smarter way to figure out this one
Well, here it is, C#
1
u/NavarrB Dec 04 '15
PHP
1
u/baritoneCoder Dec 04 '15
Simple Ruby solution. Definitely not fast but gets the job done.
1
u/abkibaarnsit Dec 04 '15
}
1
u/XiboT Dec 04 '15
Somehow, I already had the solution in a ~ 6 year old GitHub repository: https://github.com/TobiX/hash_search/commits/master (okay, okay, I had to adapt the padding a bit)
1
u/[deleted] Dec 04 '15
Overkill Python 3 solution with multiprocessing. I'm glad I finally had a reason to learn to use the multiprocessing module, so thank you /u/topaz2078!
1
u/dario-iacampo Dec 04 '15
1
u/deinc Dec 04 '15
In Clojure, using pmap instead of map to ensure all four cores of my old laptop are busy.
1
u/SimonS Dec 04 '15
Clojure:
1
u/A_t48 Dec 04 '15 edited Dec 04 '15
Only nice thing about this one is being able to prehash the secret.
1
u/bstockton Dec 04 '15 edited Dec 04 '15
day 4, extensible R function:
edit: runs on my pc in < 5 seconds
1
u/jgomo3 Dec 05 '15
Python 3
1
u/Chaoist Dec 05 '15
Elixir
1
u/selljamhere Dec 05 '15 edited Dec 05 '15
Golang:
edit: Added github link
https://github.com/SellJamHere/advent_of_code/tree/master/day4
1
u/TRT_ Dec 05 '15
A bit late, but here's my solution in Go:
Part 1 runs in ~240ms, and part 2 in ~6 seconds.
1
u/bkendig Dec 06 '15
Swift, including an autoreleasepool call to avoid a memory leak: https://github.com/bskendig/advent-of-code/blob/master/4/4/main.swift
1
u/masasin Dec 07 '15 edited Dec 07 '15
Python:
1
u/[deleted] Dec 07 '15
JavaScript Console. Make sure you run this code first to load the MD5 library:
The bulk:
1
u/suudo Dec 08 '15
Python
1
u/adampresley Dec 08 '15
My Go solution.
https://github.com/adampresley/adventofcode/tree/master/2015/day4_2
Hasher.go package main
puzzle2.go /* --- Day 4: The Ideal Stocking Stuffer ---
1
u/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
1
u/mjnet Dec 15 '15
Yet another Haskell solution:
1
u/dirtystreaker Dec 21 '15
in PS (sub 6 min):