Manuel Bustillo
8c4e6a0109
All checks were successful
Run unit tests / unit_tests (push) Successful in 3m36s
Reviewed-on: #8 Co-authored-by: Manuel Bustillo <bustikiller@bustikiller.com> Co-committed-by: Manuel Bustillo <bustikiller@bustikiller.com>
29 lines
656 B
Ruby
29 lines
656 B
Ruby
module Tables
|
|
class Swap
|
|
private attr_reader :initial_solution
|
|
def initialize(initial_solution)
|
|
@initial_solution = initial_solution
|
|
end
|
|
|
|
def each
|
|
@initial_solution.tables.combination(2) do |table_a, table_b|
|
|
table_a.product(table_b).each do |(person_a, person_b)|
|
|
table_a.delete(person_a)
|
|
table_b.delete(person_b)
|
|
|
|
table_a << person_b
|
|
table_b << person_a
|
|
|
|
yield(@initial_solution)
|
|
ensure
|
|
table_a.delete(person_b)
|
|
table_b.delete(person_a)
|
|
|
|
table_a << person_a
|
|
table_b << person_b
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|