-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay14.java
129 lines (102 loc) · 3.08 KB
/
Day14.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package aoc16;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import myutils16.StaticUtils;
public class Day14 {
private final String salt = "ihaygndm";
public int run(boolean part2) {
Map<String, Integer> quintupleIndexMapping = new HashMap<>();
Map<Integer, String> tripleIndexMapping = new HashMap<>();
List<String> hashList = new ArrayList<>();
int saltIndex = 0;
for (int i = 0; i <= 1000; i++) {
String hash = "";
if (part2) {
hash = stretchedMd5(salt + saltIndex++);
} else {
hash = md5(salt + saltIndex++);
}
hashList.add(hash);
String trip = "";
if ((trip = getTriple(hash)) != null) {
tripleIndexMapping.put(i, trip);
}
String quint = "";
if ((quint = getQuintuple(hash)) != null) {
quintupleIndexMapping.put(quint, i);
}
}
int keys = 0;
int targetIndex = 0;
int currentIndex = 0;
while (keys < 64) {
if (tripleIndexMapping.containsKey(currentIndex)) {
String quint = tripleIndexMapping.get(currentIndex).substring(0, 2)
+ tripleIndexMapping.get(currentIndex);
if (quintupleIndexMapping.containsKey(quint) && quintupleIndexMapping.get(quint) < (currentIndex + 1000)
&& quintupleIndexMapping.get(quint) > currentIndex) {
keys++;
targetIndex = currentIndex;
}
}
String nextHash = "";
if (part2) {
nextHash = stretchedMd5(salt + saltIndex);
} else {
nextHash = md5(salt + saltIndex);
}
String trip = "";
if ((trip = getTriple(nextHash)) != null) {
tripleIndexMapping.put(saltIndex, trip);
}
String quint = "";
if ((quint = getQuintuple(nextHash)) != null) {
quintupleIndexMapping.put(quint, saltIndex);
}
hashList.add(nextHash);
saltIndex++;
currentIndex++;
}
return targetIndex;
}
private String getTriple(String hash) {
for (int i = 0; i < hash.length() - 2; i++) {
if (hash.charAt(i) == hash.charAt(i + 1) && hash.charAt(i) == hash.charAt(i + 2)) {
return hash.charAt(i) + "" + hash.charAt(i) + hash.charAt(i);
}
}
return null;
}
private String getQuintuple(String hash) {
for (int i = 0; i < hash.length() - 4; i++) {
if (hash.charAt(i) == hash.charAt(i + 1) && hash.charAt(i) == hash.charAt(i + 2)
&& hash.charAt(i) == hash.charAt(i + 3) && hash.charAt(i) == hash.charAt(i + 4)) {
return hash.charAt(i) + "" + hash.charAt(i) + hash.charAt(i) + hash.charAt(i) + hash.charAt(i);
}
}
return null;
}
private String md5(String toHash) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
toHash = StaticUtils.toHexString(md.digest(toHash.getBytes()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return toHash;
}
private String stretchedMd5(String toHash) {
for (int i = 0; i < 2017; i++) {
toHash = StaticUtils.md5(toHash);
}
return toHash;
}
public static void main(String[] args) {
Day14 test = new Day14();
System.out.println(test.run(true));
}
}