module Tables class Distribution attr_reader :tables def initialize(min_per_table:, max_per_table:) @min_per_table = min_per_table @max_per_table = max_per_table end def random_distribution(people) @tables = [] @tables << people.slice!(0..rand(@min_per_table..@max_per_table)) while people.any? end def discomfort @tables.map do |table| local_discomfort(table) end.sum end def inspect "#{@tables.count} tables, discomfort: #{discomfort}" end def pretty_print @tables.map.with_index do |table, i| "Table #{i + 1} (#{table.count} ppl): (#{local_discomfort(table)}) #{table.map(&:full_name).join(', ')}" end.join("\n") end private def local_discomfort(table) 10 * (number_of_groups(table) - 1) end def number_of_groups(table) table.map do |person| person.affinity_groups end.flatten.uniq.count end end end