From 2ee9e082a2fcefe36eee2375eefd3016198d8c5b Mon Sep 17 00:00:00 2001
From: marcantoinem <marc-antoine.m@outlook.com>
Date: Wed, 7 Aug 2024 21:17:43 -0400
Subject: [PATCH] fix: improve merge algorithm

---
 aep-schedule-generator/src/data/group.rs      | 6 ++----
 aep-schedule-generator/src/data/time/hours.rs | 4 ++++
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/aep-schedule-generator/src/data/group.rs b/aep-schedule-generator/src/data/group.rs
index 3d6dbde..36417d5 100644
--- a/aep-schedule-generator/src/data/group.rs
+++ b/aep-schedule-generator/src/data/group.rs
@@ -31,8 +31,7 @@ impl Group {
                 let new_hour = new_period.hours | p.hours;
                 let is_mergeable = p.day == new_period.day
                     && p.room == new_period.room
-                    && (p.hours.start() == new_hour.start() + 4
-                        || p.hours.end() + 4 == new_hour.end());
+                    && (p.hours.is_contiguous(new_hour));
                 let keep = !is_mergeable || new_hours == Hours::default();
                 if is_mergeable {
                     new_hours |= new_hour;
@@ -49,8 +48,7 @@ impl Group {
                         let new_hour = new_period.hours | p.hours;
                         p.day == new_period.day
                             && p.room == new_period.room
-                            && (p.hours.start() == new_hour.start() + 4
-                                || p.hours.end() + 4 == new_hour.end())
+                            && (p.hours.is_contiguous(new_hour))
                     })
                     .unwrap();
                 first_period.hours |= new_hours;
diff --git a/aep-schedule-generator/src/data/time/hours.rs b/aep-schedule-generator/src/data/time/hours.rs
index be21f74..2901ec4 100644
--- a/aep-schedule-generator/src/data/time/hours.rs
+++ b/aep-schedule-generator/src/data/time/hours.rs
@@ -53,6 +53,10 @@ impl Hours {
         (self.end() >> 2) + 8
     }
 
+    pub fn is_contiguous(self, other: Self) -> bool {
+        self.start().abs_diff(other.start()) <= 5 || self.end().abs_diff(other.end()) <= 5
+    }
+
     pub fn starting_text(self) -> String {
         let mut hour = self.starting_hour().to_string();
         let minute = self.start_minutes();
-- 
GitLab