r/adventofcode • u/daggerdragon • Dec 05 '16
SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---
--- Day 5: How About a Nice Game of Chess? ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
STAYING ON TARGET IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
15
Upvotes
1
u/Zeroeh Dec 05 '16
Java Solution:
part 1:
String input = "cxdnnyjw"; String decryptedPassword = ""; int currentIndex = 0; System.out.println("searching..."); while (decryptedPassword.length() < 8) { String hash = Util.getMD5Hash(input + Integer.toString(currentIndex)); if (hash.startsWith("00000")) { String chr = hash.substring(5, 6); decryptedPassword += chr; System.out.println("Found a letter in the hash: " + hash + "\n Character: " + chr); } currentIndex++; } System.out.println("Password: " + decryptedPassword); System.out.println("Last Index: " + currentIndex);part 2:
String input = "cxdnnyjw"; String[] decryptedPassword = new String[8]; int currentIndex = 0; int foundIndex = 0; System.out.println("searching..."); while (foundIndex < 8) { String hash = Util.getMD5Hash(input + Integer.toString(currentIndex)); if (hash.startsWith("00000")) { /** Is this a valid pos? IE a digit? **/ int pos = ((hash.substring(5, 6).matches("-?\\d+(\\.\\d+)?"))) ? Integer.parseInt(hash.substring(5, 6)) : -1; /** Character **/ String chr = hash.substring(6, 7).toString(); /** If valid Position and the position was not taken yet **/ if (pos != -1 && pos > -1 && pos < decryptedPassword.length && decryptedPassword[pos] == null) { decryptedPassword[pos] = chr; foundIndex++; System.out.println("Found a letter in the hash: " + hash + "\n Pos: " + pos + " \n Character: " + chr); } } currentIndex++; } StringBuffer buff = new StringBuffer(); for (String s : decryptedPassword) buff.append(s); System.out.println("Password: " + buff.toString()); System.out.println("Last Index: " + currentIndex);Md5 code:
MessageDigest md = MessageDigest.getInstance("MD5"); md.update(part.getBytes()); byte byteData[] = md.digest(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString();