Created
October 28, 2015 13:12
-
-
Save jaeming/867098c80d1d04c047b6 to your computer and use it in GitHub Desktop.
Enigma machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Plugboard | |
def initialize(wires = '') | |
@wires = wires | |
validate unless @wires.empty? | |
end | |
def validate | |
return raise "wire error" unless @wires.is_a?(String) | |
return raise "wire error" if @wires.chars.uniq! | |
return raise "wire error" if @wires.chars.size > 20 | |
return raise "wire error" unless @wires.chars.size.even? | |
end | |
def process(wire) | |
enigma = {} | |
@wires.chars.each_with_index{|item, index| enigma[item] = @wires.chars[(index + 1)] unless index.odd?} | |
enigma[wire] || enigma.select { |key, value| value == wire }.keys[0] || wire | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment