backend by zatzou
This commit is contained in:
parent
e55f8cfd22
commit
fa7f748392
1 changed files with 49 additions and 1 deletions
50
src/main.rs
50
src/main.rs
|
@ -1,3 +1,51 @@
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::collections::btree_map::Entry;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
let dice = [6, 6, 6, 6, 7];
|
||||||
|
|
||||||
|
// i need 2 otherwise the for loops below break
|
||||||
|
let comb = combinations(&dice);
|
||||||
|
let comb2 = comb.clone();
|
||||||
|
|
||||||
|
// below code gets total for all values
|
||||||
|
let mut total = 0;
|
||||||
|
|
||||||
|
|
||||||
|
for i in comb {
|
||||||
|
total += i.1;
|
||||||
|
}
|
||||||
|
for i in comb2 {
|
||||||
|
println!("{i:?}");
|
||||||
|
println!("{total}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn combinations(dice: &[u32]) -> BTreeMap<u32, u32> {
|
||||||
|
let mut out = BTreeMap::new();
|
||||||
|
|
||||||
|
recursive(dice, &mut out, &[]);
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn recursive(dice: &[u32], out: &mut BTreeMap<u32, u32>, cur: &[u32]) {
|
||||||
|
let d = dice[0];
|
||||||
|
|
||||||
|
for i in 1..=d {
|
||||||
|
if dice.len() >= 2 {
|
||||||
|
recursive(&dice[1..], out, &[cur, &[i]].concat());
|
||||||
|
} else {
|
||||||
|
let temp = cur.iter().sum::<u32>() + i;
|
||||||
|
|
||||||
|
match out.entry(temp) {
|
||||||
|
Entry::Vacant(e) => {
|
||||||
|
e.insert(1);
|
||||||
|
},
|
||||||
|
Entry::Occupied(e) => {
|
||||||
|
*e.into_mut() += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue