Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Last active December 15, 2015 07:09
Show Gist options
  • Select an option

  • Save zeroeth/5221495 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/5221495 to your computer and use it in GitHub Desktop.
enigma fun
// rotors
int[][] NOTCHES = {
// Identity rotor:
{ 0 }, // A
// Standard rotors:
{ 17 }, // R
{ 5 }, // F
{ 22 }, // W
{ 10 }, // K
{ 0 }, // A
// The last three rotors have two step notches each.
{ 0, 13 }, // A and N
{ 0, 13 }, // A and N
{ 0, 13 }, // A and N
};
// print out rotor info
int[] rotorIndices = [1,2,3,4] // your args into the function
// loop over them using for, taking their value as the position in another array.
// this is shorthand for the longer for loop http://www.leepoint.net/notes-java/flow/loops/foreach.html
for (int index : rotorIndices) {
int[] notchList = NOTCHES[index];
for (int notchPosition : notchList) {
// here you can print out each individual value 'notchPosition'
}
}
// 8 wants you to store information about the rotors movement in their slots.
// so far you have the rotors, their list of letters, their notches.
// normally you would read a rotor by going
int rotorNumber = 1;
int index = 14;
ROTORS[rotorNumber].charAt(index); // return me letter at position 14 on rotor 1
// or two array brackets if you converted the string into an array.
// if you have an offset you can also do
int offset = 2;
ROTORS[rotorNumber].charAt(index+offset); // return me letter at position 16 on rotor 1
// this is slightly bad since if offset+index is beyond end.
// you can use modulous to loop back around to the beginning.
char[] rotorLetters = ['a', 'b', 'c'];
rotorLetters[0]; // returns a
rotorLetters[1]; // returns b
rotorLetters[2]; // returns c
rotorLetters[3]; // NO
rotorLetters[3 % 3]; // returns a
rotorLetters[4 % 3]; // returns b
// so if I had a for loop with an offset, and a mod, I can run through the letters from any starting point.
for (int i = 0; i < rotorLetters.length; i++) {
rotorLetters[(i + offset) % rotorLetters.length];
}
// thats how youll access the wheels for encoding a character.
// rotating will just be a matter of storing these offsets in an array, one for each wheel.
// advancing will just include checking if, after rotating a wheel, the offset is the same position as a notch, and if so rotate the one to your left.
// then checking to see if his new offset is the position of a notch, and if so rotating the one to his left.. and so on.
// each rotate just adds 1 to a rotorOffset (or sets it back to 0 if its gone full circle)
// each advance at most will rotate 1 or all the wheels once (if they all hit a notch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment