diff --git a/src/main.rs b/src/main.rs index e7a11a9..1053996 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,51 @@ +use std::collections::BTreeMap; +use std::collections::btree_map::Entry; + 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 { + let mut out = BTreeMap::new(); + + recursive(dice, &mut out, &[]); + + out +} + +fn recursive(dice: &[u32], out: &mut BTreeMap, 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::() + i; + + match out.entry(temp) { + Entry::Vacant(e) => { + e.insert(1); + }, + Entry::Occupied(e) => { + *e.into_mut() += 1; + } + } + } + } +} \ No newline at end of file