Skip to content

Commit

Permalink
2019: Port gcd to reusable library
Browse files Browse the repository at this point in the history
Day 10 and, soon to be, Day 12 need the greatest common divisor.
  • Loading branch information
ericvw committed May 19, 2024
1 parent 6ad68be commit df22692
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
11 changes: 3 additions & 8 deletions 2019/src/bin/puzzle_10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ use std::collections::HashMap;
use std::io;

use aoc2019::grid::Point;
use aoc2019::num;

#[derive(Ord, PartialOrd, Eq, PartialEq)]
struct AsteroidMetadata {
loc: Point,
distance: u32,
}

fn gcd(mut a: i32, mut b: i32) -> i32 {
while b != 0 {
let t = b;
b = a % b;
a = t;
}

a.abs()
fn gcd(a: i32, b: i32) -> i32 {
num::gcd(a.into(), b.into()).try_into().unwrap()
}

fn distances_from(
Expand Down
1 change: 1 addition & 0 deletions 2019/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod grid;
pub mod intcode;
pub mod iter;
pub mod num;
9 changes: 9 additions & 0 deletions 2019/src/num.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn gcd(mut a: i64, mut b: i64) -> u64 {
while b != 0 {
let t = b;
b = a % b;
a = t;
}

a.abs().try_into().unwrap()
}

0 comments on commit df22692

Please sign in to comment.