Solve rust/clock

This commit is contained in:
Avery Winters 2023-10-11 12:46:32 -05:00
parent 01407451de
commit da1f927d22
Signed by: avery
SSH key fingerprint: SHA256:eesvLB5MMqHLZrAMFt6kEhqJWnASMLcET6Sgmw0FqZI
2 changed files with 112 additions and 56 deletions

View file

@ -1,11 +1,120 @@
pub struct Clock; use std::fmt::{Display, Formatter, self};
const HOURS_PER_DAY: i32 = 24;
const MINUTES_PER_HOUR: i32 = 60;
type MinutesSinceMidnight = RingI32<{ HOURS_PER_DAY * MINUTES_PER_HOUR }>;
const HOUR_SINCE_MIDNIGHT: MinutesSinceMidnight = MinutesSinceMidnight::new(MINUTES_PER_HOUR);
#[derive(Debug, PartialEq)]
pub struct Clock {
since_midnight: MinutesSinceMidnight,
}
impl Clock { impl Clock {
pub fn new(hours: i32, minutes: i32) -> Self { pub fn new(hours: i32, minutes: i32) -> Self {
todo!("Construct a new Clock from {hours} hours and {minutes} minutes"); let since_midnight = MinutesSinceMidnight::new(minutes);
let since_midnight = HOUR_SINCE_MIDNIGHT * hours + since_midnight;
Self {
since_midnight,
}
} }
pub fn add_minutes(&self, minutes: i32) -> Self { pub fn add_minutes(&self, minutes: i32) -> Self {
todo!("Add {minutes} minutes to existing Clock time"); let since_midnight = self.since_midnight + minutes;
Self {
since_midnight,
}
}
}
impl Display for Clock {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let hours = self.since_midnight / HOUR_SINCE_MIDNIGHT;
let minutes = self.since_midnight % HOUR_SINCE_MIDNIGHT;
write!(f, "{hours:02}:{minutes:02}")
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
struct RingI32<const N: i32>(i32);
impl<const N: i32> RingI32<N> {
const fn new(v: i32) -> Self {
Self(v.rem_euclid(N))
}
const fn val(&self) -> i32 {
self.0
}
}
impl<const N: i32> Display for RingI32<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl<const N: i32> std::ops::Add<i32> for RingI32<N> {
type Output = Self;
fn add(self, rhs: i32) -> Self {
let lhs = self.val();
let rhs = rhs.rem_euclid(N);
Self((lhs + rhs).rem_euclid(N))
}
}
impl<const N: i32> std::ops::Mul<i32> for RingI32<N> {
type Output = Self;
fn mul(self, rhs: i32) -> Self {
let lhs = self.val();
let rhs = rhs.rem_euclid(N);
Self((lhs * rhs).rem_euclid(N))
}
}
impl<const N: i32> std::ops::Div<i32> for RingI32<N> {
type Output = Self;
fn div(self, rhs: i32) -> Self {
let lhs = self.val();
let rhs = rhs.rem_euclid(N);
Self((lhs / rhs).rem_euclid(N))
}
}
impl<const N: i32> std::ops::Rem<i32> for RingI32<N> {
type Output = Self;
fn rem(self, rhs: i32) -> Self {
let lhs = self.val();
let rhs = rhs.rem_euclid(N);
Self((lhs % rhs).rem_euclid(N))
}
}
impl<const N: i32> std::ops::Add for RingI32<N> {
type Output = Self;
fn add(self, rhs: Self) -> Self {
self + rhs.val()
}
}
impl<const N: i32> std::ops::Mul for RingI32<N> {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
self * rhs.val()
}
}
impl<const N: i32> std::ops::Div for RingI32<N> {
type Output = Self;
fn div(self, rhs: Self) -> Self {
self / rhs.val()
}
}
impl<const N: i32> std::ops::Rem for RingI32<N> {
type Output = Self;
fn rem(self, rhs: Self) -> Self {
self % rhs.val()
} }
} }

View file

@ -10,127 +10,106 @@ fn on_the_hour() {
} }
#[test] #[test]
#[ignore]
fn past_the_hour() { fn past_the_hour() {
assert_eq!(Clock::new(11, 9).to_string(), "11:09"); assert_eq!(Clock::new(11, 9).to_string(), "11:09");
} }
#[test] #[test]
#[ignore]
fn midnight_is_zero_hours() { fn midnight_is_zero_hours() {
assert_eq!(Clock::new(24, 0).to_string(), "00:00"); assert_eq!(Clock::new(24, 0).to_string(), "00:00");
} }
#[test] #[test]
#[ignore]
fn hour_rolls_over() { fn hour_rolls_over() {
assert_eq!(Clock::new(25, 0).to_string(), "01:00"); assert_eq!(Clock::new(25, 0).to_string(), "01:00");
} }
#[test] #[test]
#[ignore]
fn hour_rolls_over_continuously() { fn hour_rolls_over_continuously() {
assert_eq!(Clock::new(100, 0).to_string(), "04:00"); assert_eq!(Clock::new(100, 0).to_string(), "04:00");
} }
#[test] #[test]
#[ignore]
fn sixty_minutes_is_next_hour() { fn sixty_minutes_is_next_hour() {
assert_eq!(Clock::new(1, 60).to_string(), "02:00"); assert_eq!(Clock::new(1, 60).to_string(), "02:00");
} }
#[test] #[test]
#[ignore]
fn minutes_roll_over() { fn minutes_roll_over() {
assert_eq!(Clock::new(0, 160).to_string(), "02:40"); assert_eq!(Clock::new(0, 160).to_string(), "02:40");
} }
#[test] #[test]
#[ignore]
fn minutes_roll_over_continuously() { fn minutes_roll_over_continuously() {
assert_eq!(Clock::new(0, 1723).to_string(), "04:43"); assert_eq!(Clock::new(0, 1723).to_string(), "04:43");
} }
#[test] #[test]
#[ignore]
fn hours_and_minutes_roll_over() { fn hours_and_minutes_roll_over() {
assert_eq!(Clock::new(25, 160).to_string(), "03:40"); assert_eq!(Clock::new(25, 160).to_string(), "03:40");
} }
#[test] #[test]
#[ignore]
fn hours_and_minutes_roll_over_continuously() { fn hours_and_minutes_roll_over_continuously() {
assert_eq!(Clock::new(201, 3001).to_string(), "11:01"); assert_eq!(Clock::new(201, 3001).to_string(), "11:01");
} }
#[test] #[test]
#[ignore]
fn hours_and_minutes_roll_over_to_exactly_midnight() { fn hours_and_minutes_roll_over_to_exactly_midnight() {
assert_eq!(Clock::new(72, 8640).to_string(), "00:00"); assert_eq!(Clock::new(72, 8640).to_string(), "00:00");
} }
#[test] #[test]
#[ignore]
fn negative_hour() { fn negative_hour() {
assert_eq!(Clock::new(-1, 15).to_string(), "23:15"); assert_eq!(Clock::new(-1, 15).to_string(), "23:15");
} }
#[test] #[test]
#[ignore]
fn negative_hour_roll_over() { fn negative_hour_roll_over() {
assert_eq!(Clock::new(-25, 00).to_string(), "23:00"); assert_eq!(Clock::new(-25, 00).to_string(), "23:00");
} }
#[test] #[test]
#[ignore]
fn negative_hour_roll_over_continuously() { fn negative_hour_roll_over_continuously() {
assert_eq!(Clock::new(-91, 00).to_string(), "05:00"); assert_eq!(Clock::new(-91, 00).to_string(), "05:00");
} }
#[test] #[test]
#[ignore]
fn negative_minutes() { fn negative_minutes() {
assert_eq!(Clock::new(1, -40).to_string(), "00:20"); assert_eq!(Clock::new(1, -40).to_string(), "00:20");
} }
#[test] #[test]
#[ignore]
fn negative_minutes_roll_over() { fn negative_minutes_roll_over() {
assert_eq!(Clock::new(1, -160).to_string(), "22:20"); assert_eq!(Clock::new(1, -160).to_string(), "22:20");
} }
#[test] #[test]
#[ignore]
fn negative_minutes_roll_over_continuously() { fn negative_minutes_roll_over_continuously() {
assert_eq!(Clock::new(1, -4820).to_string(), "16:40"); assert_eq!(Clock::new(1, -4820).to_string(), "16:40");
} }
#[test] #[test]
#[ignore]
fn negative_sixty_minutes_is_prev_hour() { fn negative_sixty_minutes_is_prev_hour() {
assert_eq!(Clock::new(2, -60).to_string(), "01:00"); assert_eq!(Clock::new(2, -60).to_string(), "01:00");
} }
#[test] #[test]
#[ignore]
fn negative_one_twenty_minutes_is_two_prev_hours() { fn negative_one_twenty_minutes_is_two_prev_hours() {
assert_eq!(Clock::new(1, -120).to_string(), "23:00"); assert_eq!(Clock::new(1, -120).to_string(), "23:00");
} }
#[test] #[test]
#[ignore]
fn negative_hour_and_minutes_both_roll_over() { fn negative_hour_and_minutes_both_roll_over() {
assert_eq!(Clock::new(-25, -160).to_string(), "20:20"); assert_eq!(Clock::new(-25, -160).to_string(), "20:20");
} }
#[test] #[test]
#[ignore]
fn negative_hour_and_minutes_both_roll_over_continuously() { fn negative_hour_and_minutes_both_roll_over_continuously() {
assert_eq!(Clock::new(-121, -5810).to_string(), "22:10"); assert_eq!(Clock::new(-121, -5810).to_string(), "22:10");
} }
#[test] #[test]
#[ignore]
fn zero_hour_and_negative_minutes() { fn zero_hour_and_negative_minutes() {
assert_eq!(Clock::new(0, -22).to_string(), "23:38"); assert_eq!(Clock::new(0, -22).to_string(), "23:38");
} }
@ -140,112 +119,96 @@ fn zero_hour_and_negative_minutes() {
// //
#[test] #[test]
#[ignore]
fn add_minutes() { fn add_minutes() {
let clock = Clock::new(10, 0).add_minutes(3); let clock = Clock::new(10, 0).add_minutes(3);
assert_eq!(clock.to_string(), "10:03"); assert_eq!(clock.to_string(), "10:03");
} }
#[test] #[test]
#[ignore]
fn add_no_minutes() { fn add_no_minutes() {
let clock = Clock::new(6, 41).add_minutes(0); let clock = Clock::new(6, 41).add_minutes(0);
assert_eq!(clock.to_string(), "06:41"); assert_eq!(clock.to_string(), "06:41");
} }
#[test] #[test]
#[ignore]
fn add_to_next_hour() { fn add_to_next_hour() {
let clock = Clock::new(0, 45).add_minutes(40); let clock = Clock::new(0, 45).add_minutes(40);
assert_eq!(clock.to_string(), "01:25"); assert_eq!(clock.to_string(), "01:25");
} }
#[test] #[test]
#[ignore]
fn add_more_than_one_hour() { fn add_more_than_one_hour() {
let clock = Clock::new(10, 0).add_minutes(61); let clock = Clock::new(10, 0).add_minutes(61);
assert_eq!(clock.to_string(), "11:01"); assert_eq!(clock.to_string(), "11:01");
} }
#[test] #[test]
#[ignore]
fn add_more_than_two_hours_with_carry() { fn add_more_than_two_hours_with_carry() {
let clock = Clock::new(0, 45).add_minutes(160); let clock = Clock::new(0, 45).add_minutes(160);
assert_eq!(clock.to_string(), "03:25"); assert_eq!(clock.to_string(), "03:25");
} }
#[test] #[test]
#[ignore]
fn add_across_midnight() { fn add_across_midnight() {
let clock = Clock::new(23, 59).add_minutes(2); let clock = Clock::new(23, 59).add_minutes(2);
assert_eq!(clock.to_string(), "00:01"); assert_eq!(clock.to_string(), "00:01");
} }
#[test] #[test]
#[ignore]
fn add_more_than_one_day() { fn add_more_than_one_day() {
let clock = Clock::new(5, 32).add_minutes(1500); let clock = Clock::new(5, 32).add_minutes(1500);
assert_eq!(clock.to_string(), "06:32"); assert_eq!(clock.to_string(), "06:32");
} }
#[test] #[test]
#[ignore]
fn add_more_than_two_days() { fn add_more_than_two_days() {
let clock = Clock::new(1, 1).add_minutes(3500); let clock = Clock::new(1, 1).add_minutes(3500);
assert_eq!(clock.to_string(), "11:21"); assert_eq!(clock.to_string(), "11:21");
} }
#[test] #[test]
#[ignore]
fn subtract_minutes() { fn subtract_minutes() {
let clock = Clock::new(10, 3).add_minutes(-3); let clock = Clock::new(10, 3).add_minutes(-3);
assert_eq!(clock.to_string(), "10:00"); assert_eq!(clock.to_string(), "10:00");
} }
#[test] #[test]
#[ignore]
fn subtract_to_previous_hour() { fn subtract_to_previous_hour() {
let clock = Clock::new(10, 3).add_minutes(-30); let clock = Clock::new(10, 3).add_minutes(-30);
assert_eq!(clock.to_string(), "09:33"); assert_eq!(clock.to_string(), "09:33");
} }
#[test] #[test]
#[ignore]
fn subtract_more_than_an_hour() { fn subtract_more_than_an_hour() {
let clock = Clock::new(10, 3).add_minutes(-70); let clock = Clock::new(10, 3).add_minutes(-70);
assert_eq!(clock.to_string(), "08:53"); assert_eq!(clock.to_string(), "08:53");
} }
#[test] #[test]
#[ignore]
fn subtract_across_midnight() { fn subtract_across_midnight() {
let clock = Clock::new(0, 3).add_minutes(-4); let clock = Clock::new(0, 3).add_minutes(-4);
assert_eq!(clock.to_string(), "23:59"); assert_eq!(clock.to_string(), "23:59");
} }
#[test] #[test]
#[ignore]
fn subtract_more_than_two_hours() { fn subtract_more_than_two_hours() {
let clock = Clock::new(0, 0).add_minutes(-160); let clock = Clock::new(0, 0).add_minutes(-160);
assert_eq!(clock.to_string(), "21:20"); assert_eq!(clock.to_string(), "21:20");
} }
#[test] #[test]
#[ignore]
fn subtract_more_than_two_hours_with_borrow() { fn subtract_more_than_two_hours_with_borrow() {
let clock = Clock::new(6, 15).add_minutes(-160); let clock = Clock::new(6, 15).add_minutes(-160);
assert_eq!(clock.to_string(), "03:35"); assert_eq!(clock.to_string(), "03:35");
} }
#[test] #[test]
#[ignore]
fn subtract_more_than_one_day() { fn subtract_more_than_one_day() {
let clock = Clock::new(5, 32).add_minutes(-1500); let clock = Clock::new(5, 32).add_minutes(-1500);
assert_eq!(clock.to_string(), "04:32"); assert_eq!(clock.to_string(), "04:32");
} }
#[test] #[test]
#[ignore]
fn subtract_more_than_two_days() { fn subtract_more_than_two_days() {
let clock = Clock::new(2, 20).add_minutes(-3000); let clock = Clock::new(2, 20).add_minutes(-3000);
assert_eq!(clock.to_string(), "00:20"); assert_eq!(clock.to_string(), "00:20");
@ -256,97 +219,81 @@ fn subtract_more_than_two_days() {
// //
#[test] #[test]
#[ignore]
fn compare_clocks_for_equality() { fn compare_clocks_for_equality() {
assert_eq!(Clock::new(15, 37), Clock::new(15, 37)); assert_eq!(Clock::new(15, 37), Clock::new(15, 37));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_a_minute_apart() { fn compare_clocks_a_minute_apart() {
assert_ne!(Clock::new(15, 36), Clock::new(15, 37)); assert_ne!(Clock::new(15, 36), Clock::new(15, 37));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_an_hour_apart() { fn compare_clocks_an_hour_apart() {
assert_ne!(Clock::new(14, 37), Clock::new(15, 37)); assert_ne!(Clock::new(14, 37), Clock::new(15, 37));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_hour_overflow() { fn compare_clocks_with_hour_overflow() {
assert_eq!(Clock::new(10, 37), Clock::new(34, 37)); assert_eq!(Clock::new(10, 37), Clock::new(34, 37));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_hour_overflow_by_several_days() { fn compare_clocks_with_hour_overflow_by_several_days() {
assert_eq!(Clock::new(99, 11), Clock::new(3, 11)); assert_eq!(Clock::new(99, 11), Clock::new(3, 11));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_negative_hour() { fn compare_clocks_with_negative_hour() {
assert_eq!(Clock::new(-2, 40), Clock::new(22, 40)); assert_eq!(Clock::new(-2, 40), Clock::new(22, 40));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_negative_hour_that_wraps() { fn compare_clocks_with_negative_hour_that_wraps() {
assert_eq!(Clock::new(-31, 3), Clock::new(17, 3)); assert_eq!(Clock::new(-31, 3), Clock::new(17, 3));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_negative_hour_that_wraps_multiple_times() { fn compare_clocks_with_negative_hour_that_wraps_multiple_times() {
assert_eq!(Clock::new(-83, 49), Clock::new(13, 49)); assert_eq!(Clock::new(-83, 49), Clock::new(13, 49));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_minutes_overflow() { fn compare_clocks_with_minutes_overflow() {
assert_eq!(Clock::new(0, 1441), Clock::new(0, 1)); assert_eq!(Clock::new(0, 1441), Clock::new(0, 1));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_minutes_overflow_by_several_days() { fn compare_clocks_with_minutes_overflow_by_several_days() {
assert_eq!(Clock::new(2, 4322), Clock::new(2, 2)); assert_eq!(Clock::new(2, 4322), Clock::new(2, 2));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_negative_minute() { fn compare_clocks_with_negative_minute() {
assert_eq!(Clock::new(3, -20), Clock::new(2, 40)); assert_eq!(Clock::new(3, -20), Clock::new(2, 40));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_negative_minute_that_wraps() { fn compare_clocks_with_negative_minute_that_wraps() {
assert_eq!(Clock::new(5, -1490), Clock::new(4, 10)); assert_eq!(Clock::new(5, -1490), Clock::new(4, 10));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_negative_minute_that_wraps_multiple() { fn compare_clocks_with_negative_minute_that_wraps_multiple() {
assert_eq!(Clock::new(6, -4305), Clock::new(6, 15)); assert_eq!(Clock::new(6, -4305), Clock::new(6, 15));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_negative_hours_and_minutes() { fn compare_clocks_with_negative_hours_and_minutes() {
assert_eq!(Clock::new(-12, -268), Clock::new(7, 32)); assert_eq!(Clock::new(-12, -268), Clock::new(7, 32));
} }
#[test] #[test]
#[ignore]
fn compare_clocks_with_negative_hours_and_minutes_that_wrap() { fn compare_clocks_with_negative_hours_and_minutes_that_wrap() {
assert_eq!(Clock::new(-54, -11_513), Clock::new(18, 7)); assert_eq!(Clock::new(-54, -11_513), Clock::new(18, 7));
} }
#[test] #[test]
#[ignore]
fn compare_full_clock_and_zeroed_clock() { fn compare_full_clock_and_zeroed_clock() {
assert_eq!(Clock::new(24, 0), Clock::new(0, 0)); assert_eq!(Clock::new(24, 0), Clock::new(0, 0));
} }