Use sets instead of arrays to represent tables
This commit is contained in:
parent
aef55abe54
commit
29d9d21916
@ -9,7 +9,7 @@ module Tables
|
||||
|
||||
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.to_a.product(table_b.to_a).each do |(person_a, person_b)|
|
||||
original_discomfort_a = table_a.reset
|
||||
original_discomfort_b = table_b.reset
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
# Copyright (C) 2024 Manuel Bustillo
|
||||
|
||||
module Tables
|
||||
class Table < Array
|
||||
class Table < Set
|
||||
attr_accessor :discomfort, :min_per_table, :max_per_table
|
||||
|
||||
def initialize(*args)
|
||||
|
@ -6,7 +6,7 @@ class Numeric
|
||||
end
|
||||
end
|
||||
|
||||
class Array
|
||||
class Set
|
||||
def to_table
|
||||
Tables::Table.new(self)
|
||||
end
|
||||
|
@ -16,17 +16,17 @@ module Tables
|
||||
context 'when there are two tables with two people each' do
|
||||
let(:initial_solution) do
|
||||
Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution|
|
||||
distribution.tables << %i[a b].to_table
|
||||
distribution.tables << %i[c d].to_table
|
||||
distribution.tables << Set[:a, :b].to_table
|
||||
distribution.tables << Set[:c, :d].to_table
|
||||
end
|
||||
end
|
||||
|
||||
it 'yields all possible swaps between the tables' do
|
||||
expect(swaps).to contain_exactly(
|
||||
[%i[a d], %i[c b]],
|
||||
[%i[b c], %i[d a]],
|
||||
[%i[a c], %i[d b]],
|
||||
[%i[b d], %i[c a]]
|
||||
[Set[:a, :d], Set[:c, :b]],
|
||||
[Set[:b, :c], Set[:d, :a]],
|
||||
[Set[:a, :c], Set[:d, :b]],
|
||||
[Set[:b, :d], Set[:c, :a]]
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -34,22 +34,22 @@ module Tables
|
||||
context 'when there are two tables with three people each' do
|
||||
let(:initial_solution) do
|
||||
Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution|
|
||||
distribution.tables << %i[a b c].to_table
|
||||
distribution.tables << %i[d e f].to_table
|
||||
distribution.tables << Set[:a, :b, :c].to_table
|
||||
distribution.tables << Set[:d, :e, :f].to_table
|
||||
end
|
||||
end
|
||||
|
||||
it 'yields all possible swaps between the tables' do
|
||||
expect(swaps).to contain_exactly(
|
||||
[%i[b c d], %i[e f a]],
|
||||
[%i[b c e], %i[f d a]],
|
||||
[%i[b c f], %i[d e a]],
|
||||
[%i[c a d], %i[e f b]],
|
||||
[%i[c a e], %i[f d b]],
|
||||
[%i[c a f], %i[d e b]],
|
||||
[%i[a b d], %i[e f c]],
|
||||
[%i[a b e], %i[f d c]],
|
||||
[%i[a b f], %i[d e c]]
|
||||
[Set[:b, :c, :d], Set[:e, :f, :a]],
|
||||
[Set[:b, :c, :e], Set[:f, :d, :a]],
|
||||
[Set[:b, :c, :f], Set[:d, :e, :a]],
|
||||
[Set[:c, :a, :d], Set[:e, :f, :b]],
|
||||
[Set[:c, :a, :e], Set[:f, :d, :b]],
|
||||
[Set[:c, :a, :f], Set[:d, :e, :b]],
|
||||
[Set[:a, :b, :d], Set[:e, :f, :c]],
|
||||
[Set[:a, :b, :e], Set[:f, :d, :c]],
|
||||
[Set[:a, :b, :f], Set[:d, :e, :c]]
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -57,26 +57,26 @@ module Tables
|
||||
context 'when there are three tables with two people each' do
|
||||
let(:initial_solution) do
|
||||
Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution|
|
||||
distribution.tables << %i[a b].to_table
|
||||
distribution.tables << %i[c d].to_table
|
||||
distribution.tables << %i[e f].to_table
|
||||
distribution.tables << Set[:a, :b].to_table
|
||||
distribution.tables << Set[:c, :d].to_table
|
||||
distribution.tables << Set[:e, :f].to_table
|
||||
end
|
||||
end
|
||||
|
||||
it 'yields all possible swaps between the tables' do
|
||||
expect(swaps).to contain_exactly(
|
||||
[%i[b c], %i[d a], %i[e f]],
|
||||
[%i[b d], %i[c a], %i[e f]],
|
||||
[%i[a c], %i[d b], %i[e f]],
|
||||
[%i[a d], %i[c b], %i[e f]],
|
||||
[%i[b e], %i[c d], %i[f a]],
|
||||
[%i[b f], %i[c d], %i[e a]],
|
||||
[%i[a e], %i[c d], %i[f b]],
|
||||
[%i[a f], %i[c d], %i[e b]],
|
||||
[%i[a b], %i[d e], %i[f c]],
|
||||
[%i[a b], %i[d f], %i[e c]],
|
||||
[%i[a b], %i[c e], %i[f d]],
|
||||
[%i[a b], %i[c f], %i[e d]]
|
||||
[Set[:b, :c], Set[:d, :a], Set[:e, :f]],
|
||||
[Set[:b, :d], Set[:c, :a], Set[:e, :f]],
|
||||
[Set[:a, :c], Set[:d, :b], Set[:e, :f]],
|
||||
[Set[:a, :d], Set[:c, :b], Set[:e, :f]],
|
||||
[Set[:b, :e], Set[:c, :d], Set[:f, :a]],
|
||||
[Set[:b, :f], Set[:c, :d], Set[:e, :a]],
|
||||
[Set[:a, :e], Set[:c, :d], Set[:f, :b]],
|
||||
[Set[:a, :f], Set[:c, :d], Set[:e, :b]],
|
||||
[Set[:a, :b], Set[:d, :e], Set[:f, :c]],
|
||||
[Set[:a, :b], Set[:d, :f], Set[:e, :c]],
|
||||
[Set[:a, :b], Set[:c, :e], Set[:f, :d]],
|
||||
[Set[:a, :b], Set[:c, :f], Set[:e, :d]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user