Skip to content

Instantly share code, notes, and snippets.

View seanhandley's full-sized avatar
🧀
Eating cheese

Sean Handley seanhandley

🧀
Eating cheese
View GitHub Profile
@seanhandley
seanhandley / golfed.rb
Last active November 15, 2015 11:46
Einstein 17: Golfed to 546 characters
# With newlines, for readability
def q(a);
a.permutation;
end;
def n(a,b,c,d);
i(a,b,c,d,1)||i(c,d,a,b,1);
end;
def i(a,b,c,d,o=0);
(0..4).any?{|i|a[i]==b&&c[i+o]==d};
end;
@seanhandley
seanhandley / permutation.rb
Created November 15, 2015 11:42
Einstein 16: Permutation
def q(a)
a.permutation
end
q([1,2,3]).to_a # => [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
@seanhandley
seanhandley / merge.rb
Created November 15, 2015 11:29
Einstein 15: Merging left_of? with implies?
# Implies
def i(a, b, c, d)
(0..4).any? do |i|
a[i] == b &&
c[i] == d
end
end
# Left of
def l(a, b, c, d)
@seanhandley
seanhandley / simpler.rb
Last active November 15, 2015 11:20
Einstein 14: Simpler option representation
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 November 15, 2015 11:13
Einstein 13: Short method names
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 November 15, 2015 09:52
Einstein 12: Pretty Display
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 November 15, 2015 09:44
Einstein 11: Putting it all together
@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 November 15, 2015 09:37
Einstein 10: Getting smarter
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 November 15, 2015 09:39
Einstein 9: Iterating
[: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 November 14, 2015 23:35
Einstein 8: Simple test
nationalities = [:german, :swedish, :british, :norwegian, :danish]
nationalities[0] == :norwegian
# => false
drinks = [:beer, :water, :milk, :coffee, :tea]
drinks[2] == :milk
# => true