Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Created May 12, 2012 22:58
Show Gist options
  • Save tjsingleton/2669604 to your computer and use it in GitHub Desktop.
Save tjsingleton/2669604 to your computer and use it in GitHub Desktop.
Dropquest 2012, Step 1
# The product of the first two digits is 24.
# The fourth digit is half of the second digit.
# The sum of the last two digits is the same as the sum of the first and third digits.
# The sum of all the digits is 26.
# The second and third digits add up to the last digit.
def number_to_digits(n)
n.to_s.split("").map(&:to_i)
end
def product_of_1st_2_digits_is_24?(digits)
digits[0] * digits[1] == 24
end
def the_4th_digit_is_half_the_2nd_digit?(digits)
digits[3] == digits[1] / 2
end
def sum_last_2_digits_is_eql_sum_1st_and_third?(digits)
digits[3] + digits[4] == digits[0] + digits[2]
end
def sum_is_26?(digits)
digits.inject(:+) == 26
end
def the_2nd_and_3rd_digits_add_up_to_last_digit?(digits)
digits[1] + digits[2] == digits[4]
end
def is_password?(n)
digits = number_to_digits(n)
product_of_1st_2_digits_is_24?(digits) && the_4th_digit_is_half_the_2nd_digit?(digits) &&
sum_last_2_digits_is_eql_sum_1st_and_third?(digits) && sum_is_26?(digits) &&
the_2nd_and_3rd_digits_add_up_to_last_digit?(digits)
end
puts (9999...100000).each.detect &method(:is_password?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment