-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
greed-is-good.js
43 lines (37 loc) · 1.19 KB
/
greed-is-good.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
function score( dice ) {
const counts = dice.reduce((counts, roll) => {
counts[roll] = counts[roll] || 0;
counts[roll]++;
return counts;
}, {});
const specialScoreValues = {
1: 100,
5: 50
};
return Object.keys(counts).reduce((score, roll) => {
const count = counts[roll];
if (count >= 3 && !specialScoreValues[roll]) {
return score + (roll * 100)
} else if (specialScoreValues[roll]) {
const specialValue = specialScoreValues[roll];
let specialScore;
if (count < 3) {
specialScore = specialValue * count;
} else {
specialScore = (specialValue * 10) + ((count - 3) * specialValue);
}
return score + specialScore;
}
return score;
}, 0);
}
// it( "should value this as worthless", function() {
console.log( score( [2, 3, 4, 6, 2] ), "Should be 0 :-(" );
// });
// it( "should value this triplet correctly", function() {
console.log( score( [4, 4, 4, 3, 3] ) == 400, "Should be 400" );
// });
// it( "should value this mixed set correctly", function() {
console.log( score( [2, 4, 4, 5, 4] ) == 450, "Should be 450" );
// });
console.log( score( [1, 1, 1, 1, 1] ) == 1000, "Should be 1000" );