wedding-planner/app/services/tables/discomfort_calculator.rb

28 lines
617 B
Ruby
Raw Normal View History

2024-10-27 21:42:45 +00:00
# Copyright (C) 2024 Manuel Bustillo
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