Created
September 16, 2015 15:25
-
-
Save karapetyan/db089784a385de94ecf7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rabbit | |
@@result = 0 | |
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 | |
decomposition(optimal_jumps) | |
puts @@result ## | |
end | |
def decomposition(jumps) | |
combinations(jumps) | |
jumps.each do |jump| | |
numbers = [] | |
if jump > 1 | |
numbers << jump | |
numbers.each do |number| | |
while number > 1 do | |
numbers[0] -= 1 | |
numbers << 1 | |
# call combinations. | |
# if i make decomposition for 5 there will never be 2 + 2 + 1 in this case | |
end | |
end | |
end | |
end | |
end | |
def dec(nums) | |
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