From caa38e535a685b4c556bde5884b17e064236a1e4 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 9 Dec 2023 13:29:39 +0100 Subject: [PATCH] =?UTF-8?q?feat(math):=20add=20=E2=80=B9gcd=E2=80=BA=20and?= =?UTF-8?q?=20=E2=80=B9lcm=E2=80=BA=20to=20the=20library?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- src/bin/day08.rs | 12 ------------ src/lib.rs | 15 +++++++++------ src/math.rs | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 src/math.rs diff --git a/src/bin/day08.rs b/src/bin/day08.rs index 7f485ee..9ae77cc 100644 --- a/src/bin/day08.rs +++ b/src/bin/day08.rs @@ -20,18 +20,6 @@ impl Direction { } } -fn gcd(mut x: u64, mut y: u64) -> u64 { - while y != 0 { - (x, y) = (y, x % y); - } - - x -} - -fn lcm(x: u64, y: u64) -> u64 { - x / gcd(x, y) * y -} - struct Day08 { instructions: Vec, graph: HashMap, diff --git a/src/lib.rs b/src/lib.rs index 609038b..0bf25a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,17 @@ #![allow(unused_imports)] +mod data_structures; +pub use data_structures::*; + mod input; pub use input::*; +mod iterators; +pub use iterators::*; + +mod math; +pub use math::*; + mod solution; pub use solution::*; @@ -11,9 +20,3 @@ pub use testing::*; mod vectors; pub use vectors::*; - -mod iterators; -pub use iterators::*; - -mod data_structures; -pub use data_structures::*; diff --git a/src/math.rs b/src/math.rs new file mode 100644 index 0000000..3c44e22 --- /dev/null +++ b/src/math.rs @@ -0,0 +1,19 @@ +use std::ops::{Div, Mul, Rem}; + +pub fn gcd(mut x: T, mut y: T) -> T +where + T: Copy + Default + PartialEq + Rem, +{ + while y != T::default() { + (x, y) = (y, x % y); + } + + x +} + +pub fn lcm(x: T, y: T) -> T +where + T: Copy + Default + PartialEq + Div + Mul + Rem, +{ + x / gcd(x, y) * y +}