Use sets instead of arrays to represent tables #112

Merged
bustikiller merged 1 commits from table-set into main 2024-11-10 16:31:49 +00:00
4 changed files with 35 additions and 35 deletions

View File

@ -9,7 +9,7 @@ module Tables
def each def each
@initial_solution.tables.combination(2) do |table_a, table_b| @initial_solution.tables.combination(2) do |table_a, table_b|
table_a.product(table_b).each do |(person_a, person_b)| table_a.to_a.product(table_b.to_a).each do |(person_a, person_b)|
original_discomfort_a = table_a.reset original_discomfort_a = table_a.reset
original_discomfort_b = table_b.reset original_discomfort_b = table_b.reset

View File

@ -1,7 +1,7 @@
# Copyright (C) 2024 Manuel Bustillo # Copyright (C) 2024 Manuel Bustillo
module Tables module Tables
class Table < Array class Table < Set
attr_accessor :discomfort, :min_per_table, :max_per_table attr_accessor :discomfort, :min_per_table, :max_per_table
def initialize(*args) def initialize(*args)

View File

@ -6,7 +6,7 @@ class Numeric
end end
end end
class Array class Set
def to_table def to_table
Tables::Table.new(self) Tables::Table.new(self)
end end

View File

@ -16,17 +16,17 @@ module Tables
context 'when there are two tables with two people each' do context 'when there are two tables with two people each' do
let(:initial_solution) do let(:initial_solution) do
Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution|
distribution.tables << %i[a b].to_table distribution.tables << Set[:a, :b].to_table
distribution.tables << %i[c d].to_table distribution.tables << Set[:c, :d].to_table
end end
end end
it 'yields all possible swaps between the tables' do it 'yields all possible swaps between the tables' do
expect(swaps).to contain_exactly( expect(swaps).to contain_exactly(
[%i[a d], %i[c b]], [Set[:a, :d], Set[:c, :b]],
[%i[b c], %i[d a]], [Set[:b, :c], Set[:d, :a]],
[%i[a c], %i[d b]], [Set[:a, :c], Set[:d, :b]],
[%i[b d], %i[c a]] [Set[:b, :d], Set[:c, :a]]
) )
end end
end end
@ -34,22 +34,22 @@ module Tables
context 'when there are two tables with three people each' do context 'when there are two tables with three people each' do
let(:initial_solution) do let(:initial_solution) do
Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution|
distribution.tables << %i[a b c].to_table distribution.tables << Set[:a, :b, :c].to_table
distribution.tables << %i[d e f].to_table distribution.tables << Set[:d, :e, :f].to_table
end end
end end
it 'yields all possible swaps between the tables' do it 'yields all possible swaps between the tables' do
expect(swaps).to contain_exactly( expect(swaps).to contain_exactly(
[%i[b c d], %i[e f a]], [Set[:b, :c, :d], Set[:e, :f, :a]],
[%i[b c e], %i[f d a]], [Set[:b, :c, :e], Set[:f, :d, :a]],
[%i[b c f], %i[d e a]], [Set[:b, :c, :f], Set[:d, :e, :a]],
[%i[c a d], %i[e f b]], [Set[:c, :a, :d], Set[:e, :f, :b]],
[%i[c a e], %i[f d b]], [Set[:c, :a, :e], Set[:f, :d, :b]],
[%i[c a f], %i[d e b]], [Set[:c, :a, :f], Set[:d, :e, :b]],
[%i[a b d], %i[e f c]], [Set[:a, :b, :d], Set[:e, :f, :c]],
[%i[a b e], %i[f d c]], [Set[:a, :b, :e], Set[:f, :d, :c]],
[%i[a b f], %i[d e c]] [Set[:a, :b, :f], Set[:d, :e, :c]]
) )
end end
end end
@ -57,26 +57,26 @@ module Tables
context 'when there are three tables with two people each' do context 'when there are three tables with two people each' do
let(:initial_solution) do let(:initial_solution) do
Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution|
distribution.tables << %i[a b].to_table distribution.tables << Set[:a, :b].to_table
distribution.tables << %i[c d].to_table distribution.tables << Set[:c, :d].to_table
distribution.tables << %i[e f].to_table distribution.tables << Set[:e, :f].to_table
end end
end end
it 'yields all possible swaps between the tables' do it 'yields all possible swaps between the tables' do
expect(swaps).to contain_exactly( expect(swaps).to contain_exactly(
[%i[b c], %i[d a], %i[e f]], [Set[:b, :c], Set[:d, :a], Set[:e, :f]],
[%i[b d], %i[c a], %i[e f]], [Set[:b, :d], Set[:c, :a], Set[:e, :f]],
[%i[a c], %i[d b], %i[e f]], [Set[:a, :c], Set[:d, :b], Set[:e, :f]],
[%i[a d], %i[c b], %i[e f]], [Set[:a, :d], Set[:c, :b], Set[:e, :f]],
[%i[b e], %i[c d], %i[f a]], [Set[:b, :e], Set[:c, :d], Set[:f, :a]],
[%i[b f], %i[c d], %i[e a]], [Set[:b, :f], Set[:c, :d], Set[:e, :a]],
[%i[a e], %i[c d], %i[f b]], [Set[:a, :e], Set[:c, :d], Set[:f, :b]],
[%i[a f], %i[c d], %i[e b]], [Set[:a, :f], Set[:c, :d], Set[:e, :b]],
[%i[a b], %i[d e], %i[f c]], [Set[:a, :b], Set[:d, :e], Set[:f, :c]],
[%i[a b], %i[d f], %i[e c]], [Set[:a, :b], Set[:d, :f], Set[:e, :c]],
[%i[a b], %i[c e], %i[f d]], [Set[:a, :b], Set[:c, :e], Set[:f, :d]],
[%i[a b], %i[c f], %i[e d]] [Set[:a, :b], Set[:c, :f], Set[:e, :d]]
) )
end end
end end