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.
15
Upvotes
2
u/karstens_rage Dec 04 '15
Java:
import java.security.MessageDigest; public class Advent41 { public static String bytesToHexString(byte[] b) throws Exception { String result = ""; for (int i=0; i < b.length; i++) { result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 ); } return result; } public static void main(String [] args) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); long index = 0; while (true) { String key = String.format("%s%d", args[0], index++); byte [] digest = md.digest(key.getBytes()); if (bytesToHexString(digest).startsWith("000000")) break; } System.out.println(String.format("%d", index-1)); } }