Files
java-competitive/archive/2016.12/2016.12.05 - unsorted/Day5.java
2019-03-15 13:47:54 +04:00

43 lines
1.1 KiB
Java

package chelper;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import io.InputReader;
import io.OutputWriter;
public class Day5 {
public static String toHex(byte[] bytes) {
BigInteger bi = new BigInteger(1, bytes);
return String.format("%0" + (bytes.length << 1) + "x", bi);
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
String s = in.nextString();
char[] res = new char[8];
int filled = 0;
for (int i = 0; true; i++) {
try {
String hash = toHex(MessageDigest.getInstance("MD5").digest((s + Integer.toString(i)).getBytes()));
if (hash.startsWith("00000")) {
int pos = hash.charAt(5) - '0';
if (pos >= 0 && pos < 8 && res[pos] == 0) {
res[pos] = hash.charAt(6);
filled += 1;
System.out.println(filled);
System.out.println(new String(res));
System.out.println();
}
}
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
}