From 3971fab1965571861c78f090865b7b44579e33ac Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 1 Aug 2024 19:24:29 +0200 Subject: [PATCH] Refactor implementation to make it more efficient --- app/services/tables/discomfort_calculator.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/tables/discomfort_calculator.rb b/app/services/tables/discomfort_calculator.rb index e1b413d..69214bb 100644 --- a/app/services/tables/discomfort_calculator.rb +++ b/app/services/tables/discomfort_calculator.rb @@ -12,13 +12,13 @@ module Tables private def cohesion_penalty - table.map { |guest| guest.affinity_group_list.first }.combination(2).sum do |a, b| + 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 1 if distance.nil? + next count_a * count_b if distance.nil? next 0 if distance.zero? - Rational(distance, distance + 1) + count_a * count_b * Rational(distance, distance + 1) end end end