Skip to content

Instantly share code, notes, and snippets.

Avatar

Sean Handley seanhandley

View GitHub Profile
@seanhandley
seanhandley / simpler.rb
Last active Nov 15, 2015
Einstein 14: Simpler option representation
View simpler.rb
colors = [:white, :yellow, :blue, :red, :green]
cigars = [:blends, :pall_mall, :prince, :bluemasters, :dunhill]
nationalities = [:german, :swedish, :british, :norwegian, :danish]
drinks = [:beer, :water, :tea, :milk, :coffee]
pets = [:birds, :cats, :horses, :fish, :dogs]
# Becomes:
c = [0,1,2,3,4]
s = [5,6,7,8,9] # Cigars becomes 's' for 'smokes'
@seanhandley
seanhandley / short.rb
Created Nov 15, 2015
Einstein 13: Short method names
View short.rb
def implies?(set_a, val_a, set_b, val_b)
(0..4).any? do |i|
set_a[i] == val_a &&
set_b[i] == val_b
end
end
# Becomes:
def i(a, b, c, d)
@seanhandley
seanhandley / pretty.rb
Created Nov 15, 2015
Einstein 12: Pretty Display
View pretty.rb
class Symbol
def humanize
result = self.to_s
result.gsub!('_', ' ')
result.split(' ').collect{|part| part.capitalize }.join(' ')
end
end
solution, attempts = solve
@seanhandley
seanhandley / solve.rb
Created Nov 15, 2015
Einstein 11: Putting it all together
View solve.rb
@colors = [:white, :yellow, :blue, :red, :green].shuffle.permutation
@cigars = [:blends, :pall_mall, :prince, :bluemasters, :dunhill].shuffle.permutation
@nationalities = [:german, :swedish, :british, :norwegian, :danish,].shuffle.permutation
@drinks = [:beer, :water, :tea, :milk, :coffee].shuffle.permutation
@pets = [:birds, :cats, :horses, :fish, :dogs].shuffle.permutation
def solve
i = 0
@colors.each do |colors|
i += 1
@seanhandley
seanhandley / smarter.rb
Created Nov 15, 2015
Einstein 10: Getting smarter
View smarter.rb
i = 0
[:white, :yellow, :blue, :red, :green].permutation.each do |colors|
[:german, :swedish, :british, :norwegian, :danish,].permutation.each do |nationalities|
i += 1
end
end
puts i
# => 14400
@seanhandley
seanhandley / iterate.rb
Last active Nov 15, 2015
Einstein 9: Iterating
View iterate.rb
[:white, :yellow, :blue, :red, :green].permutation.each do |colors|
[:german, :swedish, :british, :norwegian, :danish,].permutation.each do |nationalities|
[:birds, :cats, :horses, :fish, :dogs].permutation.each do |pets|
[:beer, :water, :tea, :milk, :coffee].permutation.each do |drinks|
[:blends, :pall_mall, :prince, :bluemasters, :dunhill].permutation.each do |cigars|
next unless nationalities[0] == :norwegian
next unless drinks[2] == :milk
next unless implies?(nationalities, :british, colors, :red)
next unless implies?(nationalities, :swedish, pets, :dogs)
@seanhandley
seanhandley / simple_test.rb
Last active Nov 14, 2015
Einstein 8: Simple test
View simple_test.rb
nationalities = [:german, :swedish, :british, :norwegian, :danish]
nationalities[0] == :norwegian
# => false
drinks = [:beer, :water, :milk, :coffee, :tea]
drinks[2] == :milk
# => true
@seanhandley
seanhandley / next_to.rb
Created Nov 14, 2015
Einstein 7: Next To
View next_to.rb
def left_of?(set_a, val_a, set_b, val_b)
(0..4).any? do |i|
set_a[i] == val_a &&
set_b[i+1] == val_b
end
end
# Instead of defining a right_of?, we can just swap the values we input to left_of?
def next_to?(set_a, val_a, set_b, val_b)
@seanhandley
seanhandley / left_of.rb
Created Nov 14, 2015
Einstein 6: Left of
View left_of.rb
def left_of?(set_a, val_a, set_b, val_b)
(0..4).any? do |i|
set_a[i] == val_a &&
set_b[i+1] == val_b
end
end
colors = [:white, :yellow, :blue, :red, :green]
left_of?(colors, :green, colors, :white)
@seanhandley
seanhandley / implies_facts.rb
Last active Nov 14, 2015
Einstein 5: All the implies
View implies_facts.rb
implies?(nationalities, :british, colors, :red)
implies?(nationalities, :swedish, pets, :dogs)
implies?(colors, :green, drinks, :coffee)
implies?(nationalities, :danish, drinks, :tea)
implies?(cigars, :pall_mall, pets, :birds)
implies?(nationalities , :german, cigars, :prince)
implies?(colors, :yellow, cigars, :dunhill)
implies?(cigars, :bluemasters, drinks, :beer)
You can’t perform that action at this time.