-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp059.java
More file actions
39 lines (33 loc) · 1.12 KB
/
p059.java
File metadata and controls
39 lines (33 loc) · 1.12 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
package level03;
import org.junit.Test;
import lib.EulerTest;
public class p059 extends EulerTest {
final int N = 3;
/**
* Find the sum of the ASCII values of the decrypted text, encrypted by repeatedly xor-ing with
* a N-letter key consisting of letters from a to z.
* <p>
* Find the key that yields the maximum number of spaces in the decrypted message.
*/
@Test
public void test() {
long[] encrypted = readAsGrid(",")[0];
int[] key = new int[N];
for (int slice : range(N)) {
int maxNumSpaces = 0;
for (char keychar = 'a'; keychar <= 'z'; keychar++) {
int numSpaces = 0;
for (int i = slice; i < encrypted.length; i += N)
if ((encrypted[i] ^ keychar) == ' ')
numSpaces++;
if (numSpaces > maxNumSpaces) {
maxNumSpaces = numSpaces;
key[slice] = keychar;
}
}
}
for (int i : range(encrypted.length))
ans += encrypted[i] ^ key[i % N];
check(129448);
}
}