Skip to content

Instantly share code, notes, and snippets.

@karapetyan
Created September 14, 2015 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karapetyan/7613792b3e412520c696 to your computer and use it in GitHub Desktop.
Save karapetyan/7613792b3e412520c696 to your computer and use it in GitHub Desktop.
class Rabbit
@@result = 0
@@hsh = {}
def initialize(path = "./input")
File.open(path, "r") do |file|
file.each_line do |line|
line = line.split
short_path(line[0].to_i, line[1].to_i)
end
end
end
private
def short_path(jump, total)
optimal_jumps = []
while total >= jump do
total -= jump
optimal_jumps << jump
end
optimal_jumps << total if total > 0
melt(optimal_jumps)
puts @@result
end
def melt(numbers)
combinations(numbers)
arr_numbers = []
numbers.each { |val| arr_numbers << ([] << val)}
arr_numbers.each_index do |index|
if arr_numbers[index][0] > 1 && arr_numbers[index][0] > arr_numbers[index][1].to_i
arr_numbers[index][0] -= 1
arr_numbers[index][1] == nil ? arr_numbers[index][1] = 1 : arr_numbers[index][1] += 1
if @@hsh[arr_numbers.flatten] == nil
@@hsh[arr_numbers.flatten] = 1
melt(arr_numbers.flatten)
end
end
end
end
def combinations(jumps)
same = {}
factorial_arr = []
jumps.each { |jump| same[jump] == nil ? same[jump] = 1 : same[jump] += 1 }
n = (1..jumps.count).inject(1, :*)
same.values.each { |value| factorial_arr << (1..value).inject(1, :*) }
r = factorial_arr.inject(:*)
@@result += (n / r)
end
end
Rabbit.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment