diff --git a/app/services/tables/wheel_swap.rb b/app/services/tables/wheel_swap.rb index d932537..b5171a1 100644 --- a/app/services/tables/wheel_swap.rb +++ b/app/services/tables/wheel_swap.rb @@ -10,7 +10,12 @@ module Tables end def call - @initial_solution.deep_dup + new_solution = @initial_solution.deep_dup + + selected_guests = new_solution.tables.map(&:pop).cycle.tap(&:next) + new_solution.tables.each { |table| table << selected_guests.next } + + new_solution end end end \ No newline at end of file diff --git a/config/initializers/ruby_extensions.rb b/config/initializers/ruby_extensions.rb index 47091de..594fa59 100644 --- a/config/initializers/ruby_extensions.rb +++ b/config/initializers/ruby_extensions.rb @@ -10,4 +10,10 @@ class Set def to_table Tables::Table.new(self) end + + def pop + element = self.to_a.sample + self.delete(element) + element + end end \ No newline at end of file diff --git a/spec/services/tables/wheel_swap_spec.rb b/spec/services/tables/wheel_swap_spec.rb index 19c6b0b..20913fd 100644 --- a/spec/services/tables/wheel_swap_spec.rb +++ b/spec/services/tables/wheel_swap_spec.rb @@ -15,6 +15,13 @@ module Tables result = described_class.new(initial_solution).call expect(result.tables.size).to eq(3) + expect(result.tables.map(&:size)).to all(eq(3)) + + expect(result.tables).not_to include(initial_solution.tables[0]) + expect(result.tables).not_to include(initial_solution.tables[1]) + expect(result.tables).not_to include(initial_solution.tables[2]) + + expect(result.tables.map(&:to_a).flatten).to contain_exactly(*(:a..:i).to_a) end end end