Skip to content

Instantly share code, notes, and snippets.

@fogonthedowns
Created July 28, 2014 03:22
Show Gist options
  • Save fogonthedowns/2187ac8a7707a93a3854 to your computer and use it in GitHub Desktop.
Save fogonthedowns/2187ac8a7707a93a3854 to your computer and use it in GitHub Desktop.
Hacking a Lock
class Hack
attr_accessor :possible, :array, :final_answer
def initialize
@possible = ["A","B","C","D"]
@array = []
@final_answer = []
self.generate
self.process_array
end
def generate
self.possible.each do |letter|
self.possible.each do |two|
self.possible.each do |three|
self.possible.each do |four|
@array << letter
@array << two
@array << three
@array << four
end
end
end
end
end
def process_array
string = self.array.join("")
first_string = self.array.drop(1).join("")
second_string = self.array.drop(2).join("")
third_string = self.array.drop(3).join("")
string.scan(/.{3}/).each{|x|final_answer<<[x]}
first_string.scan(/.{3}/).each{|x|final_answer<<[x]}
second_string.scan(/.{3}/).each{|x|final_answer<<[x]}
third_string.scan(/.{3}/).each{|x|final_answer<<[x]}
@final_answer.uniq!
@final_answer = @final_answer.join.to_s
end
def tell_me_the_answer
puts "the minimum number of characters you must enter to ensure that you can break the lock is #{@final_answer.size}"
puts "or #{@final_answer.size/3} 3 character combinations"
puts "and the sequence that will break the lock is this:"
puts @final_answer
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment