Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Last active November 17, 2020 21:09
Show Gist options
  • Save seanhandley/0a61e0b31babaced890a591ad7fb8800 to your computer and use it in GitHub Desktop.
Save seanhandley/0a61e0b31babaced890a591ad7fb8800 to your computer and use it in GitHub Desktop.
Finding Raffle Numbers @ RubyConf2020
# Greedy Approach with Array#product
#
# $ time ruby a.rb
# Found it! Your raffle number is f8ryt
# real 2m12.019s
# user 2m7.764s
# sys 0m3.383s
require 'digest'
def generate_hash(id, raffle_number)
Digest::MD5.base64digest(id+ raffle_number)
end
def mine_raffle_number(id, digest)
alphabet = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9)
alphabet.product(alphabet, alphabet, alphabet, alphabet).each do |raffle_candidate|
raffle_candidate = raffle_candidate.join("")
if generate_hash(id, raffle_candidate) == digest
puts "Found it! Your raffle number is #{raffle_candidate}"
return
end
end
puts "not found"
end
id = "https://www.crowdcast.io/seanhandley256"
digest = "VMAZY9CRQYA9JnFCbXETjg=="
mine_raffle_number(id, digest)
# Lazy Approach with Array#permutation
#
# $ time ruby b.rb
# Found it! Your raffle number is f8ryt
# real 0m16.604s
# user 0m16.352s
# sys 0m0.129s
require 'digest'
def generate_hash(id, raffle_number)
Digest::MD5.base64digest(id+ raffle_number)
end
def mine_raffle_number(id, digest)
alphabet = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9)
alphabet.permutation(5).each do |raffle_candidate|
raffle_candidate = raffle_candidate.join("")
if generate_hash(id, raffle_candidate) == digest
puts "Found it! Your raffle number is #{raffle_candidate}"
return
end
end
puts "not found"
end
id = "https://www.crowdcast.io/seanhandley256"
digest = "VMAZY9CRQYA9JnFCbXETjg=="
mine_raffle_number(id, digest)
# Benchmark just to see how long it takes to build the collection up front
#
# $ time ruby c.rb
# real 1m26.778s
# user 1m23.649s
# sys 0m2.611s
alphabet = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9)
alphabet.product(alphabet, alphabet, alphabet, alphabet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment