Created
September 14, 2015 07:51
-
-
Save karapetyan/7613792b3e412520c696 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
3 7 |
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 | |
@@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