-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution00_brute_force.js
54 lines (45 loc) · 1.29 KB
/
solution00_brute_force.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
///
/// count(S,D) Count how many configurations of D dice sum S
///
/// Brute force solution
/// Test is performed for 1 to 12 dice only
///
/// Algorithm fails when the sum of a dice configuration exceeds the floating point precission
///
/// Author: Antonio Cabrera Perez (2022-01-30)
main();
function main() {
for (let dice = 1; dice <= 12; dice++) {
let sum = Math.floor((dice + 6 * dice) / 2);
console.time("time");
console.log({ sum, dice, result: count(sum, dice) });
console.timeEnd("time");
console.log("");
}
}
function count(s, d) {
let count = 0
enumerateAllVariationsSums(d)(sum => {
if (sum === s) count++;
})
return count
}
function enumerateAllVariationsSums(total_dice) {
return (consumeFunc) => {
let diceVector = Array(total_dice).fill(0);
let lastSum = 0;
do {
consumeFunc(lastSum + total_dice);
setNextVariation(diceVector)
lastSum = diceVector.reduce((t, s) => t + s, 0);
} while (lastSum !== 0)
}
function setNextVariation(diceVector) {
// <diceVector> is treated as a base 6 number: next vector is generated adding 1 to the actual one
for (let die = 0; die < diceVector.length; die++) {
diceVector[die] = (diceVector[die] + 1) % 6;
if (diceVector[die] != 0)
break;
}
}
}