Skip to content

Instantly share code, notes, and snippets.

@nateborr
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save nateborr/caf44fa8b8c99e26ce3c to your computer and use it in GitHub Desktop.

Select an option

Save nateborr/caf44fa8b8c99e26ce3c to your computer and use it in GitHub Desktop.
beer for cory
#!/usr/bin/env ruby
# Here is an alphabet including a beer mug
beer = "\u{1F37A}"
letters = ('a'..'z').to_a << beer
# Here are the words for the beer numbers
number_words = %w{one two three four five}
# Here are the number words padded out to equal lengths with prepended beers
max_number_word_length = number_words.map(&:length).max
padded_number_words = number_words.map { |number_word| beer * (max_number_word_length - number_word.length) + number_word }
# Regex matchers for the number words with any number of intermediating characters
regexes = padded_number_words.map {|pnw| Regexp.new pnw.split('').join('.*')}
# Make a random string and get the data for any regex that matches it
random_string = 500.times.map { letters.sample }.join
match_hash = regexes.map {|regex| regex.match(random_string) }.each_with_index.inject({}){|memo, i| memo[i[1] + 1] = i[0]; memo}
match_hash.delete_if{|k,v| !v}
# For each matching regex, get the number of characters required before the match was complete
match_hash.keys.each {|k| match_hash[k] = ((split = random_string.split(match_hash[k][0])).size < 2) ? random_string.length : match_hash[k][0].length + split[0].length }
fastest_matches = match_hash.reject {|k,v| v != match_hash.values.min }
# Here is some jazz for drama
random_string.split('').each do |letter|
sleep (1..50).to_a.sample.to_f / 1000
print (letter == beer) ? letter : '.'
end
# Randomly select one of the fastest-matching number words, if any, and output the corresponding integer
fastest_matches.keys
puts "\nDrink beer no. #{fastest_matches.keys.sample}" || "I DONT KNOW CORY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment