見習村12 - Greed is Dice

12 - Greed is Dice

Don’t say so much, just coding…

Instruction

Greed is a dice game played with five six-sided dice. Your mission, should you choose to accept it, is to score a throw according to these rules. You will always be given an array with five six-sided dice values.

1
2
3
4
5
6
7
8
Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 point

A single die can only be counted once in each roll. For example, a given “5” can only count as part of a triplet (contributing to the 500 points) or as a single 50 points, but not both in the same roll.

Example scoring

1
2
3
4
5
Throw       Score
--------- ------------------
5 1 3 4 1 250: 50 (for the 5) + 2 * 100 (for the 1s)
1 1 1 3 1 1100: 1000 (for three 1s) + 100 (for the other 1)
2 4 4 5 4 450: 400 (for three 4s) + 50 (for the 5)

In some languages, it is possible to mutate the input to the function. This is something that you should never do. If you mutate the input, you will not be able to pass all the tests.

Ruby

Init

1
2
3
def score( dice )
# Fill me in!
end

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
describe "Scorer Function" do
it "should value this as worthless" do
Test.expect( score( [2, 3, 4, 6, 2] ) == 0, "Should be 0 :-(" );
end

it "should value this triplet correctly" do
Test.expect( score( [2, 2, 2, 3, 3] ) == 200, "Should be 200" );
end

it "should value this mixed set correctly" do
Test.expect( score( [2, 4, 4, 5, 4] ) == 450, "Should be 450" );
end
end

Javascript

Init

1
2
3
function score( dice ) {
// Fill me in!
}

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
describe( "Scorer Function", function() {
it( "should value this as worthless", function() {
Test.expect( score( [2, 3, 4, 6, 2] ) == 0, "Should be 0 :-(" );
});

it( "should value this triplet correctly", function() {
Test.expect( score( [4, 4, 4, 3, 3] ) == 400, "Should be 400" );
});

it( "should value this mixed set correctly", function() {
Test.expect( score( [2, 4, 4, 5, 4] ) == 450, "Should be 450" );
});
});

Thinking

想法(1):
想法(2):

圖片—
圖片來源:Unsplash Marvin Meyer

Hint & Reference

Solution

Ruby

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
# Solution 1
def score(dice)
[
dice.count(1) / 3 * 1000,
dice.count(6) / 3 * 600,
dice.count(5) / 3 * 500,
dice.count(4) / 3 * 400,
dice.count(3) / 3 * 300,
dice.count(2) / 3 * 200,
dice.count(1) % 1 * 100,
dice.count(5) % 5 * 50,
].reduce(:+)
end

# Solution 2
def score (dice)
sum = 0
(1..6).each do |i|
count = dice.select { |d| d == i }.size

sum += (i==1 ? 1000 : i*100) if count >= 3
sum += (count % 3) * 100 if i == 1
sum += (count % 3) * 50 if i == 5
end

sum
end

Javascript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Solution 1
function score(dice) {
var sum = 0
for(i=1; i<=6; i++){
var count = dice.filter( d => d == i).length
if(count >= 3){
sum += (i==1 ? 1000 : i*100)
}
if(i == 1){
sum += (count % 3) * 100
}
if(i == 5){
sum += (count % 3) * 50
}
}
return sum
}