From d89579d483a1748f74b5416ff53a9929b1b22623 Mon Sep 17 00:00:00 2001 From: Gary Yendell Date: Fri, 21 Oct 2022 23:22:05 +0100 Subject: [PATCH] Exercism 7 - Calculate rem with total minutes and convert back --- clock/src/lib.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/clock/src/lib.rs b/clock/src/lib.rs index 26bb9b7..94d041b 100644 --- a/clock/src/lib.rs +++ b/clock/src/lib.rs @@ -1,7 +1,7 @@ use std::fmt; -const HOURS_PER_DAY: i32 = 24; -const MINUTES_PER_HOUR: i32 = 60; +const MINS_PER_HOUR: i32 = 60; +const MINS_PER_DAY: i32 = MINS_PER_HOUR * 24; #[derive(Debug, PartialEq, Eq)] pub struct Clock { @@ -10,18 +10,12 @@ pub struct Clock { } impl Clock { - pub fn new(new_hours: i32, new_minutes: i32) -> Self { - let mut minutes = new_minutes % MINUTES_PER_HOUR; - let mut hours = new_hours + new_minutes / MINUTES_PER_HOUR; - - if minutes < 0 { - hours -= 1; - minutes += 60; - } + pub fn new(hours: i32, minutes: i32) -> Self { + let total_minutes = (minutes + hours * MINS_PER_HOUR).rem_euclid(MINS_PER_DAY); Clock { - hours: hours.rem_euclid(HOURS_PER_DAY), - minutes, + hours: total_minutes / MINS_PER_HOUR, + minutes: total_minutes % MINS_PER_HOUR, } }