Greed is Good

SungJunEun·2021년 11월 7일
0

Codewars 문제풀이

목록 보기
10/26
post-thumbnail

Description:

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.

 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

 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.

My solution:

function score( dice ) {
// get array of counts of each numberlet countNumber = [];
  let score = 0;
  for(i=0; i<6; i++){
     countNumber[i] = dice.filter(element => element == (i+1)).length;
  }
// function for 1, 5function doubleEvent(array, i, big, small) {
    if(array[i]>=3) {
      score+=big;
      countNumber[i] = countNumber[i] - 3;
    }
    score+=countNumber[i] * small;
  }

// function for restfunction soleEvent(array, i, reward) {
    if(array[i]>=3) {
      score+=reward;
    }
  }

  doubleEvent(countNumber, 0, 1000, 100);
  doubleEvent(countNumber, 4, 500, 50);
  soleEvent(countNumber, 1, 200);
  soleEvent(countNumber, 2, 300);
  soleEvent(countNumber, 3, 400);
  soleEvent(countNumber, 5, 600);

  return score;
}

Best solutions:

function score( dice ) {
  var dc = [0,0,0,0,0,0];
  var tdr = [1000,200,300,400,500,600];
  var sdr = [100,0,0,0,50,0];

	// count for each number
  dice.forEach(function(x){ dc[x-1]++; });
	// add score
  return dc.reduce(function(s,x,i){ 
    return s + (x >= 3? tdr[i] : 0) + sdr[i]*(x % 3);
  },0);
}
  • 각 숫자 나온 빈도 세기
    // dice = [1,2,2,2,3,4]
    dice.forEach(x => dc[x-1]++);
    // dc = [1,3,1,1,0]

나의 코드에 비하여 굉장히 간단하다.

  • 점수 합치기
    // dc = [1,3,1,1,0]
    dc.reduce(function(acc,cur,i){
    		return acc + (cur>=3 ? tdr[i] : 0) + sdr[i] * (cur % 3); 
    						
    	},0);
  • cur - 3이 아닌 cur % 3을 이용한게 매우 좋은 생각같다.
  • reduce 메서드는 값을 합칠 때 매우 유용하기 때문에 항상 염두할 필요가 있을 것 같다.
profile
블록체인 개발자(진)

0개의 댓글