Package net.n1da.dev.euler
Class PE0059
- java.lang.Object
-
- net.n1da.dev.euler.core.Problem
-
- net.n1da.dev.euler.PE0059
-
- All Implemented Interfaces:
Solvable
public class PE0059 extends Problem
Solution for problem 59 "XOR decryption" completed on Fri, 16 Dec 2016, 07:28.- Since:
- 10.12.2016 08:10:12
- Author:
- Nico Danneberg
- See Also:
- Problem @ Project Euler, Solution @ Ni-Da-Net
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]
code
A memory for the codeprivate static double[]
StandardDistribution
Stores the standard distribution of letters in the English language
-
Method Summary
Modifier and Type Method Description private byte[]
decode(byte[] code, byte[] key)
Decodes a given code by the given key using a bitwise XOR operation.private double[]
getLetterDist(byte[] text)
Calculates the distribution of all letters in the given text.void
prepare()
Prepares the problem by reading the code from a given file.java.lang.String
solve()
All possible keys from [aaa] to [zzz] are generated.-
Methods inherited from class net.n1da.dev.euler.core.Problem
finish, getResult, getRuntime, toString
-
-
-
-
Method Detail
-
prepare
public void prepare()
Prepares the problem by reading the code from a given file.
-
solve
public java.lang.String solve()
All possible keys from [aaa] to [zzz] are generated. The code is decoded by every key and the distribution of letters is calculated for every of these decoded texts. The text with the smallest deviation is the solution.- Returns:
- the sum of the ASCII codes of all letters in the decoded text
-
decode
private byte[] decode(byte[] code, byte[] key)
Decodes a given code by the given key using a bitwise XOR operation.- Parameters:
code
- the code as array of byteskey
- the key as array of bytes- Returns:
- the decoded text as the result of decoding
-
getLetterDist
private double[] getLetterDist(byte[] text)
Calculates the distribution of all letters in the given text. Both, lower and upper case versions of a letter, are counted as one.- Parameters:
text
- the text to read the letters from- Returns:
- an 26 elements big array with the relative count of each letter
-
-