Skip to content

Instantly share code, notes, and snippets.

@Pomeha
Last active February 19, 2020 10:04
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 Pomeha/31085ca4438e2397442dd762cd87b029 to your computer and use it in GitHub Desktop.
Save Pomeha/31085ca4438e2397442dd762cd87b029 to your computer and use it in GitHub Desktop.
Решения задачи про гири. Условие: Сумма весов гирь с левой стороны никогда не будет равна сумме весов гирь с правой
class WeightTask
def call
max = 32
(1..max-6).each do |a|
(a.next..max-5).each do |b|
(b.next..max-4).each do |c|
(c.next..max-3).each do |d|
(d.next..max-2).each do |e|
(e.next..max-1).each do |f|
array = [a, b, c, d, e, f]
next unless combo_wombo(array)
if array.max < max
max = array.max
p array
end
end
end
end
end
end
end
end
def combo_wombo(array)
size = array.size
(1..size).each do |combo_size|
prepared_array = array.permutation(combo_size).to_a
prepared_array.each_with_index do |el, index|
memo_array = Array.new(prepared_array)
memo_array.flatten!.uniq!
el.each { |m_a| memo_array.delete m_a }
ar_size = memo_array.size
(1..ar_size).each do |sub_combosize|
memo_array.permutation(sub_combosize).to_a.each do |combi|
return false if el.inject(0, :+) == combi.inject(0, :+)
end
end
end
end
array
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment