Greed is Good

Instructions

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 “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 50 + 2 * 100 = 250
1 1 1 3 1 1000 + 100 = 1100
2 4 4 5 4 400 + 50 = 450

My Solution

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
import java.util.Arrays;

public class GreedIsGood {
public static int greedy(int[] dice){
int result = 0;

int numberOf1 = howManyNumberInArray(dice, 1);
int numberOf2 = howManyNumberInArray(dice, 2);
int numberOf3 = howManyNumberInArray(dice, 3);
int numberOf4 = howManyNumberInArray(dice, 4);
int numberOf5 = howManyNumberInArray(dice, 5);
int numberOf6 = howManyNumberInArray(dice, 6);

if (numberOf1 < 3) {
result += 100*numberOf1;
} else if (numberOf1 >= 3) {
result += 1000 + 100*(numberOf1 - 3);
}

if (numberOf5 < 3) {
result += 50*numberOf5;
} else if (numberOf5 >= 3) {
result += 500 + 50*(numberOf5 - 3);
}

if (numberOf2 >= 3)
result += 200;
if (numberOf3 >= 3)
result += 300;
if (numberOf4 >= 3)
result += 400;
if (numberOf6 >= 3)
result += 600;

return result;
}

public static int howManyNumberInArray(int[] dice, int target) {
return (int) Arrays.stream(dice).filter(number -> number == target).count();
}
}

Better Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
public class GreedIsGoodSolution {

public static int greedy(int[] dice) {
int[] n = new int[7];

for (int i : dice) {
n[i]++;
}

return (n[1]/3)*1000 + (n[1]%3)*100 + (n[2]/3)*200 + (n[3]/3)*300 +
(n[4]/3)*400 + (n[5]/3)*500 + (n[5]%3)*50 + (n[6]/3)*600;
}
}

Feeling

진짜 위 솔루션을 보자마자 감탄할 수 밖에 없었다..

더 할말이 없고 나도 이런 코드를 짤 수 있게 노력해야겠다.

Share