Skip to content

Instantly share code, notes, and snippets.

@jaeming
Created October 28, 2015 13:12
Show Gist options
  • Save jaeming/867098c80d1d04c047b6 to your computer and use it in GitHub Desktop.
Save jaeming/867098c80d1d04c047b6 to your computer and use it in GitHub Desktop.
Enigma machine
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