Skip to content

Instantly share code, notes, and snippets.

@DoubleCouponDay
Last active August 16, 2019 01:37
Show Gist options
  • Select an option

  • Save DoubleCouponDay/287b261e24dad943528e0ceca4ff6c66 to your computer and use it in GitHub Desktop.

Select an option

Save DoubleCouponDay/287b261e24dad943528e0ceca4ff6c66 to your computer and use it in GitHub Desktop.
referenced by discord server
fn main() {
let output = game(204);
assert_eq!(output, vec![20808]);
}
pub fn game(n: u64) -> Vec<u64> {
println!("input: {}", n);
if n == 0 {
return vec![n];
}
//add all fractions together
let mut runningtotal: Vec<u64> = vec![0, 0];
let mut sumloopbegan = true;
for i in 1..=n { //rows
for o in 1..=n { //columns
let currenttile = vec![i, (i + o)];
if sumloopbegan {
sumloopbegan = false;
runningtotal = currenttile;
continue;
}
sumoftwofractions(currenttile, &mut runningtotal);
}
}
println!("summed fractions: {} / {}", runningtotal[0], runningtotal[1]);
//if the resulting fraction is completely simplified, return numerator in vec
if runningtotal[1] == 1 {
runningtotal.truncate(1);
}
runningtotal
}
fn sumoftwofractions(mut toadd: Vec<u64>, output: &mut Vec<u64>) -> () {
let lowestcommondenominator: u64 = findlowestcommondenominator(toadd[1], output[1]);
let multiplierone: u64 = lowestcommondenominator / toadd[1];
let multipliertwo: u64 = lowestcommondenominator / output[1];
toadd[0] *= multiplierone;
toadd[1] *= multiplierone;
output[0] *= multipliertwo;
let summednumerator: u64 = toadd[0] + output[0];
output[0] = summednumerator;
reducefraction(output);
}
fn findlowestcommondenominator(denominatorone: u64, denominatortwo: u64) -> u64 {
let mut index: f64 = 1.0;
let floatone = denominatorone as f64;
let floattwo = denominatortwo as f64;
while index % floatone != 0.0 ||
index % floattwo != 0.0 {
index += 1.0;
}
index as u64
}
fn reducefraction(input: &mut Vec<u64>) -> () {
let highestdivider = findhighestcommondivider(input[0], input[1]);
input[0] /= highestdivider;
input[1] /= highestdivider;
}
fn findhighestcommondivider(one: u64, two: u64) -> u64 {
let startingindex: u64 = one.min(two);
let mut output = 1;
println!("startingindex: {}", startingindex);
let range = (1..=startingindex).rev();
for i in range {
if one % i == 0 && two % i == 0 {
output = i;
return output;
}
}
println!("highestdivider: {}", output);
output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment