forked from sherxon/AlgoDS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnotHash_10.java
More file actions
102 lines (92 loc) · 2.97 KB
/
Copy pathKnotHash_10.java
File metadata and controls
102 lines (92 loc) · 2.97 KB
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
package adventofcode;
import java.util.ArrayList;
import java.util.List;
/**
* Why Did you create this class? what does it do?
http://adventofcode.com/2017/day/14
*/
@SuppressWarnings("Duplicates") public class KnotHash_10 {
public static void main(String[] args) {
//List<Integer> list = Arrays.asList(227, 169, 3, 166, 246, 201, 0, 47, 1, 255, 2, 254, 96, 3, 97, 144);
//System.out.println(solve(list));
System.out.println(calculateKnotHash("227,169,3,166,246,201,0,47,1,255,2,254,96,3,97,144"));
}
public static String calculateKnotHash(String s) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
list.add((int) s.charAt(i));
}
list.add(17);
list.add(31);
list.add(73);
list.add(47);
list.add(23);
int[] a = new int[256];
for (int i = 0; i < a.length; i++)
a[i] = i;
int skipSize = 0;
int current = 0;
for (int p = 0; p < 64; p++) {
for (int i = 0; i < list.size(); i++) {
int length = list.get(i);
int[] reversed = reverse(a, current, length);
int k = current;
for (int j = 0; j < reversed.length; j++) {
a[k % a.length] = reversed[j];
k++;
}
current += skipSize + length;
skipSize++;
}
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 256; i++) {
int xor = a[i];
for (int j = i + 1; j < i + 16; j++) {
xor ^= a[j];
}
i += 15;
String ss = Integer.toHexString(xor);
builder.append(ss.length() == 1 ? "0" + ss : ss);
}
return builder.toString();
}
private static int solve(List<Integer> list) {
if (list == null || list.isEmpty())
return 0;
int[] a = new int[256];
for (int i = 0; i < a.length; i++)
a[i] = i;
int skipSize = 0;
int current = 0;
for (int i = 0; i < list.size(); i++) {
int length = list.get(i);
int[] reversed = reverse(a, current, length);
int k = current;
for (int j = 0; j < reversed.length; j++) {
a[k % a.length] = reversed[j];
k++;
}
current += skipSize + length;
skipSize++;
}
return a[0] * a[1];
}
private static int[] reverse(int[] a, int current, int length) {
int[] rev = new int[length];
int k = 0;
for (int i = current; i < current + length; i++) {
rev[k++] = a[i % a.length];
}
int i = 0;
int j = rev.length - 1;
while (i < j) {
int temp = rev[i];
rev[i] = rev[j];
rev[j] = temp;
i++;
j--;
}
return rev;
}
}