feat(math): add ‹gcd› and ‹lcm› to the library
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
88d06b99ec
commit
caa38e535a
3 changed files with 28 additions and 18 deletions
|
@ -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 {
|
struct Day08 {
|
||||||
instructions: Vec<Direction>,
|
instructions: Vec<Direction>,
|
||||||
graph: HashMap<String, (String, String)>,
|
graph: HashMap<String, (String, String)>,
|
||||||
|
|
15
src/lib.rs
15
src/lib.rs
|
@ -1,8 +1,17 @@
|
||||||
#![allow(unused_imports)]
|
#![allow(unused_imports)]
|
||||||
|
|
||||||
|
mod data_structures;
|
||||||
|
pub use data_structures::*;
|
||||||
|
|
||||||
mod input;
|
mod input;
|
||||||
pub use input::*;
|
pub use input::*;
|
||||||
|
|
||||||
|
mod iterators;
|
||||||
|
pub use iterators::*;
|
||||||
|
|
||||||
|
mod math;
|
||||||
|
pub use math::*;
|
||||||
|
|
||||||
mod solution;
|
mod solution;
|
||||||
pub use solution::*;
|
pub use solution::*;
|
||||||
|
|
||||||
|
@ -11,9 +20,3 @@ pub use testing::*;
|
||||||
|
|
||||||
mod vectors;
|
mod vectors;
|
||||||
pub use vectors::*;
|
pub use vectors::*;
|
||||||
|
|
||||||
mod iterators;
|
|
||||||
pub use iterators::*;
|
|
||||||
|
|
||||||
mod data_structures;
|
|
||||||
pub use data_structures::*;
|
|
||||||
|
|
19
src/math.rs
Normal file
19
src/math.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
use std::ops::{Div, Mul, Rem};
|
||||||
|
|
||||||
|
pub fn gcd<T>(mut x: T, mut y: T) -> T
|
||||||
|
where
|
||||||
|
T: Copy + Default + PartialEq + Rem<Output = T>,
|
||||||
|
{
|
||||||
|
while y != T::default() {
|
||||||
|
(x, y) = (y, x % y);
|
||||||
|
}
|
||||||
|
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lcm<T>(x: T, y: T) -> T
|
||||||
|
where
|
||||||
|
T: Copy + Default + PartialEq + Div<Output = T> + Mul<Output = T> + Rem<Output = T>,
|
||||||
|
{
|
||||||
|
x / gcd(x, y) * y
|
||||||
|
}
|
Loading…
Reference in a new issue