<commit_before>// var App = Ember.Application.create();

// App.ApplicationController = Ember.Controller.extend();
// App.ApplicationView = Ember.View.extend({
//   templateName: 'application'
// });

// App.Router = Ember.Router.extend({
//   root: Ember.Route.extend({
//     index: Ember.Route.extend({
//       route: '/'
//     })
//   })
// })

// App.initialize();

/**
 * The enigma?
 */
function Enigma() {
  this.init();
}

Enigma.prototype.init = function (rotorSettings) {
  this.initRotors(rotorSettings);
};

Enigma.prototype.initRotors = function (rotorSettings) {
  var ciphers = [
      // ABCDEFGHIJKLMNOPQRSTUVWXYZ
        'BDFHJLCPRTXVZNYEIWGAKMUSQO', // 1930, Enigma I
        'AJDKSIRUXBLHWTMCQGZNPYFVOE', // 1930, Enigma I
        'EKMFLGDQVZNTOWYHXUSPAIBRCJ', // 1930, Enigma I
        // 'ESOVPZJAYQUIRHXLNFTGKDCMWB', // DEC 1938, M3 Army
        // 'VZBRGITYUPSDNHLXAWMJQOFECK', // DEC 1938, M3 Army
        // 'JPG<KEY>', // 1939, M3 & M4 Naval (FEB 1942)
        // 'NZJHGRCXMYSWBOUFAIVLPEKQDT', // 1939, M3 & M4 Naval (FEB 1942)
        // 'FKQHTLXOCBJSPDZRAMEWNIUYGV'  // 1939, M3 & M4 Naval (FEB 1942)
      ],
      cipher, rotor, prevRotor;

  rotorSettings = rotorSettings || [];

  for (var i = 0, len = ciphers.length; i < len; i++) {
    cipher = ciphers[i];
    rotor  = new Rotor(cipher, rotorSettings[i] || 0);

    rotor.i = i; // DEBUG

    if (i == 0) {
      this.firstRotor = rotor;
    }

    if (prevRotor) {
      prevRotor.next = rotor;
      rotor.prev     = prevRotor;
    }

    prevRotor = rotor;
  };

  rotor.next = new Reflector('YRUHQSLDPXNGOKMIEBFZCWVJAT');
  rotor.next.prev = rotor;
};

Enigma.prototype.setRotorSettings = function (rotorSettings) {
  var i = 0, rotor = this.firstRotor;

  rotorSettings = rotorSettings || [];

  while (rotor) {
    rotor.setting = rotorSettings[i] || 0;

    rotor = rotor.next;
    i++;
  }
};

Enigma.prototype.encipher = function(string) {
  var chr, val, result = [];

  string = string.toUpperCase().replace(/[^A-Z]/g, '').split('');

  for (var i = 0, len = string.length; i < len; i++) {
    chr = string[i];
    console.log(chr, 'START enciphering chr'); // DEBUG

    result.push(this.firstRotor.encipher(chr));

    this.firstRotor.rotate();
  };

  return result.join('');
}


/**
 * A rotor.
 */
function Rotor(cipher, setting) {
  this.init(cipher, setting);
}

Rotor.prototype.init = function (cipher, setting) {
  this.setting = setting;
  this.cipher  = cipher.split('');

  this.next = null;
  this.prev = null;
};

Rotor.prototype.rotate = function () {
  this.setting = (this.setting + 1) % 26;

  if (this.setting == 0 && this.next) {
    this.next.rotate();
  }
};

Rotor.prototype.encipher = function (chr, dir) {
  var encipheredChr = this.cipher[(chr.charCodeAt(0) - 65 + this.setting) % 26] || 0;

  dir = typeof dir === 'boolean' ? dir : true;

  console.log([dir ? 'FORWARD' : 'BACKWARD', chr, chr.charCodeAt(0) - 65, this.setting, '-> ' + encipheredChr], 'Rotor #' + this.i); // DEBUG

  if (dir) { // Forward
    if (this.next) {
      return this.next.encipher(encipheredChr, dir);
    }
  } else {  // Reverse
    if (this.prev) {
      return this.prev.encipher(encipheredChr, dir);
    }
  }

  return encipheredChr;
};

Rotor.prototype.decipher = function (chr) {
  var decipheredChr = (this.cipher.indexOf(chr) + this.setting) % 26 || 0;

  console.log(['BACKWARD', chr, chr.charCodeAt(0) - 65, this.setting, '-> ' + decipheredChr], 'Rotor #' + this.i); // DEBUG

  if (this.prev) {
    return this.prev.decipher(decipheredChr);
  }

  return decipheredChr;
};

/**
 * The reflector at the end of the rotor set
 */
function Reflector(cipher) {
  this.cipher  = cipher.split('');

  this.prev = null;
}

Reflector.prototype.rotate = function () {
  // Spins freely, no action taken
};

Reflector.prototype.encipher = function (chr) {
  var mappedChr = this.cipher[(chr.charCodeAt(0) - 65) % 26];

  console.log(['REFLECTOR', chr, chr.charCodeAt(0) - 65, '-> ' + mappedChr], 'Reflector'); // DEBUG

  return this.prev.encipher(mappedChr, false);
};<commit_msg>It's alive!<commit_after>// var App = Ember.Application.create();

// App.ApplicationController = Ember.Controller.extend();
// App.ApplicationView = Ember.View.extend({
//   templateName: 'application'
// });

// App.Router = Ember.Router.extend({
//   root: Ember.Route.extend({
//     index: Ember.Route.extend({
//       route: '/'
//     })
//   })
// })

// App.initialize();

/**
 * The enigma?
 */
function Enigma() {
  this.init();
}

Enigma.prototype.init = function (rotorSettings) {
  this.initRotors(rotorSettings);
};

Enigma.prototype.initRotors = function (rotorSettings) {
  var ciphers = [
      // ABCDEFGHIJKLMNOPQRSTUVWXYZ
        'EKMFLGDQVZNTOWYHXUSPAIBRCJ', // 1930, Enigma I (L)
        'AJDKSIRUXBLHWTMCQGZNPYFVOE', // 1930, Enigma I (M)
        'BDFHJLCPRTXVZNYEIWGAKMUSQO', // 1930, Enigma I (R)
        // 'ESOVPZJAYQUIRHXLNFTGKDCMWB', // DEC 1938, M3 Army
        // 'VZBRGITYUPSDNHLXAWMJQOFECK', // DEC 1938, M3 Army
        // 'JPGVOUMFYQBENHZRDKASXLICTW', // 1939, M3 & M4 Naval (FEB 1942)
        // 'NZJHGRCXMYSWBOUFAIVLPEKQDT', // 1939, M3 & M4 Naval (FEB 1942)
        // 'FKQHTLXOCBJSPDZRAMEWNIUYGV'  // 1939, M3 & M4 Naval (FEB 1942)
      ],
      cipher, rotor, prevRotor;

  rotorSettings = rotorSettings || [];

  // Stack the rotors in reverse, this corresponds to how the actual machine's
  // electrical pathway went from R -> M -> L -> REFLECTOR -> L -> M -> R
  for (var i = ciphers.length - 1; i >= 0; i--) {
    cipher = ciphers[i];
    rotor  = new Rotor(cipher, rotorSettings[i] || 0);

    rotor.i = i; // DEBUG

    if (i == ciphers.length - 1) {
      this.firstRotor = rotor;
    }

    if (prevRotor) {
      prevRotor.next = rotor;
      rotor.prev     = prevRotor;
    }

    prevRotor = rotor;
  };

  //                          ABCDEFGHIJKLMNOPQRSTUVWXYZ
  rotor.next = new Reflector('YRUHQSLDPXNGOKMIEBFZCWVJAT');
  rotor.next.prev = rotor;
};

Enigma.prototype.setRotorSettings = function (rotorSettings) {
  var i = 0, rotor = this.firstRotor;

  rotorSettings = rotorSettings || [];

  while (rotor) {
    rotor.setting = rotorSettings[i] || 0;

    rotor = rotor.next;
    i++;
  }
};

Enigma.prototype.encipher = function(string) {
  var chr, val, result = [];

  string = string.toUpperCase().replace(/[^A-Z]/g, '').split('');

  for (var i = 0, len = string.length; i < len; i++) {
    chr = string[i];
    // console.log(chr, 'START enciphering chr'); // DEBUG

    result.push(this.firstRotor.encipher(chr));

    this.firstRotor.rotate();
  };

  return result.join('');
}


/**
 * A rotor.
 */
function Rotor(cipher, setting) {
  this.init(cipher, setting);
}

Rotor.prototype.init = function (cipher, setting) {
  this.setting = setting;
  this.cipher  = cipher.split('');

  this.next = null;
  this.prev = null;
};

Rotor.prototype.rotate = function () {
  this.setting = (this.setting + 1) % 26;

  if (this.setting == 0 && this.next) {
    this.next.rotate();
  }
};

Rotor.prototype.encipher = function (chr, dir) {
  var encipheredChr;

  dir = typeof dir === 'boolean' ? dir : true;

  if (dir) { // Forward
    encipheredChr = this.cipher[(chr.charCodeAt(0) - 65 + this.setting) % 26] || 0;
    // console.log([dir ? '>>' : '<<', chr + ' -> ' + encipheredChr], 'Rotor #' + this.i); // DEBUG
    if (this.next) {
      return this.next.encipher(encipheredChr, dir);
    }
  } else {  // Reverse
    var pos = this.cipher.indexOf(chr) - this.setting;
    pos = pos < 0 ? pos + 26 : pos;
    encipheredChr = String.fromCharCode(pos % 26 + 65) || 0;
    // console.log([dir ? '>>' : '<<', chr + ' -> ' + encipheredChr], 'Rotor #' + this.i); // DEBUG
    if (this.prev) {
      return this.prev.encipher(encipheredChr, dir);
    }
  }

  return encipheredChr;
};

Rotor.prototype.chrPos = function (chr) {
  return ;
}

/**
 * The reflector at the end of the rotor set
 */
function Reflector(cipher) {
  this.cipher  = cipher.split('');

  this.prev = null;
}

Reflector.prototype.rotate = function () {
  // Spins freely, no action taken
};

Reflector.prototype.encipher = function (chr) {
  var mappedChr = this.cipher[(chr.charCodeAt(0) - 65) % 26];
  // console.log(['||', chr + ' -> ' + mappedChr], 'Reflector'); // DEBUG
  return this.prev.encipher(mappedChr, false);
};
{'fraction_non_alphanumeric': 0.17224020136641496, 'fraction_numerical': 0.02001678053457989, 'mean_word_length': 2.778985507246377, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1934993', 'n_tokens_mistral': 3404, 'n_tokens_neox': 3117, 'n_words': 631}