Skip to content

Instantly share code, notes, and snippets.

@Tmw
Created September 28, 2020 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tmw/3ea3f0586170859e1de6fc802716cdd5 to your computer and use it in GitHub Desktop.
Save Tmw/3ea3f0586170859e1de6fc802716cdd5 to your computer and use it in GitHub Desktop.
struct Case<'a> {
title: &'a str,
max_amount: usize,
wanted_amounts: Vec<usize>,
}
fn main() {
// Setup the various cases
let simulations = vec![
Case {
title: "Case 1",
max_amount: 3,
wanted_amounts: vec![2, 7, 4],
},
Case {
title: "Case 2",
max_amount: 6,
wanted_amounts: vec![9, 10, 4, 7, 2],
},
];
// run the simulations
simulations
.iter()
.for_each(run_case);
}
fn run_case(case: &Case) {
let mut outputs : Vec<(usize, usize)> = case.wanted_amounts
.iter()
.enumerate()
.map(|(idx, amount)| {
(idx + 1, f64::ceil(*amount as f64 / case.max_amount as f64) as usize)
})
.collect();
// Sort by how many trips they would need to make
outputs.sort_by(|a, b| a.1.cmp(&b.1));
// grab the index from the result
let results : Vec<usize> = outputs
.iter()
.map(|output| output.0).collect();
println!("Running: {}", case.title);
println!("Outputs: {:?}", results);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment