module Tables
  class DiscomfortCalculator
    private attr_reader :table
    def initialize(table)
      @table = table
    end

    def calculate
      cohesion_penalty
    end

    private

    def cohesion_penalty
      table.map { |guest| guest.affinity_group_list.first }.tally.to_a.combination(2).sum do |(a, count_a), (b, count_b)|
        distance = AffinityGroupsHierarchy.instance.distance(a, b)

        next count_a * count_b if distance.nil?
        next 0 if distance.zero?

        count_a * count_b * Rational(distance, distance + 1)
      end
    end
  end
end