Skip to content

Commit

Permalink
Exercism 7 - Calculate rem with total minutes and convert back
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Oct 21, 2022
1 parent 1cea1d1 commit d89579d
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions clock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
}
}

Expand Down

0 comments on commit d89579d

Please sign in to comment.