Created
December 4, 2015 22:02
-
-
Save lifeofchrome/df29b9ca463307410b82 to your computer and use it in GitHub Desktop.
Day 4 of the Advent of Code 2015, most of this by markwryan, thanks to him.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by lifeofchrome on 12/4/2015. | |
* Credit to markwryan for ~99% of this solution. | |
*/ | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Day4 { | |
static final String HASH_KEY = "hash"; | |
static final String ANSWER_KEY = "answer"; | |
public static void main(String[] args) throws NoSuchAlgorithmException { | |
Map<String, String> result = calculateMD5Hash("ckczppom", "00000"); | |
System.out.println("4-1: " + result.get(ANSWER_KEY)); | |
//=======PART 2======= | |
result = calculateMD5Hash("ckczppom", "000000"); | |
System.out.println("4-2: " + result.get(ANSWER_KEY)); | |
} | |
static Map<String, String> calculateMD5Hash(String input, String prefix) throws NoSuchAlgorithmException { | |
Map<String, String> result = new HashMap<>(); | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
long i = 1; | |
StringBuilder sb = new StringBuilder(); | |
while (true) { | |
sb.delete(0, sb.length()); | |
md.update((input + i).getBytes()); | |
byte[] digest = md.digest(); | |
for (byte b : digest) { | |
sb.append(String.format("%02x", b & 0xff)); | |
} | |
if (sb.indexOf(prefix) == 0) { | |
break; | |
} | |
i++; | |
} | |
result.put(HASH_KEY, sb.toString()); | |
result.put(ANSWER_KEY, String.valueOf(i)); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment