greedGame.js

Codewars 알고리즘 풀이


Problem

  • Greed is a dice game played with five six-sided dice.
    • Greed는 6면체 주사위 5개로 하는 게임이다.
  • You will always be given an array with five six-sided dice values.
    • 5개의 주사위 값을 가진 배열이 제공된다.


Rules:

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 points



Solution 01

function greedGame(dice) {
  if (dice.length !== 5) return 0;
  
  let str = dice.sort().join('');
  let result = 0;
  
  const rules = [
    { reg: /111/, score: 1000 },
    { reg: /666/, score: 600 },
    { reg: /555/, score: 500 },
    { reg: /444/, score: 400 },
    { reg: /333/, score: 300 },
    { reg: /222/, score: 200 },
    { reg: /1/, score: 100 },
    { reg: /5/, score: 50 },
  ];
  
  rules.forEach(i => {
    while (i.reg.test(str)) {
      str = str.replace(i.reg, '');
      result += i.score;
    }
  });
  
  return result;
}

greedGame([1, 1, 1, 3, 1]);  // 1100 (1000 + 100)
greedGame([2, 4, 4, 5, 4]);  // 450 (400 + 500)
greedGame([4, 4, 4, 3, 3]);  // 400
greedGame([2, 3, 4, 6, 2]);  // 0
greedGame([5, 1, 3, 4, 1]);  // 250 (50 + 100 + 100)
greedGame([1, 2, 3, 4]);     // 0


Solution 02

function greedGame(dice) {
  if (dice.length !== 5) return 0;
  
  let one = 0, six = 0, five = 0, four = 0, three = 0, two = 0;
  let result = 0;
  let i = 0;
  
  while (i < 5) {
    if (dice[i] === 1) one++;
    if (dice[i] === 6) six++;
    if (dice[i] === 5) five++;
    if (dice[i] === 4) four++;
    if (dice[i] === 3) three++;
    if (dice[i] === 2) two++;
    i++;
  }
  
  if (one > 2) { result += 1000; one -= 3; }
  if (six > 2) { result += 600; }
  if (five > 2) { result += 500; five -= 3; }
  if (four > 2) { result += 400; }
  if (three > 2) { result += 300; }
  if (two > 2) { result += 200; }
  result += one * 100;
  result += five * 50;
  
  return result;
}

greedGame([1, 1, 1, 3, 1]);  // 1100 (1000 + 100)
greedGame([2, 4, 4, 5, 4]);  // 450 (400 + 500)
greedGame([4, 4, 4, 3, 3]);  // 400
greedGame([2, 3, 4, 6, 2]);  // 0
greedGame([5, 1, 3, 4, 1]);  // 250 (50 + 100 + 100)
greedGame([1, 2, 3, 4]);     // 0


Solution 03

function greedGame(dice) {
  if (dice.length !== 5) return 0;
  
  dice = dice.sort();
  let result = 0;
  
  for (let i = 0; i < dice.length; i++) {
    if (dice[i] === dice[i + 1] && dice[i + 1] === dice[i + 2]) {
      switch (dice[i]) {
        case 1:
          result += 1000;
          break;
        case 6:
          result += 600;
          break;
        case 5:
          result += 500;
          break;
        case 4:
          result += 400;
          break;
        case 3:
          result += 300;
          break;
        case 2:
          result += 200;
          break;
      }
      
      i = i + 2;
      continue;
    } else if (dice[i] === 1) {
      result += 100;
    } else if (dice[i] === 5) {
      result += 50;
    }
  }
  
  return result;
}

greedGame([1, 1, 1, 3, 1]);  // 1100 (1000 + 100)
greedGame([2, 4, 4, 5, 4]);  // 450 (400 + 500)
greedGame([4, 4, 4, 3, 3]);  // 400
greedGame([2, 3, 4, 6, 2]);  // 0
greedGame([5, 1, 3, 4, 1]);  // 250 (50 + 100 + 100)
greedGame([1, 2, 3, 4]);     // 0