DEV Community

Discussion on: Daily Challenge #97 - Greed is Good

Collapse
 
erezwanderman profile image
erezwanderman

Javascript!

const score = rolls => {
  const rollsOfNum = [1, 2, 3, 4, 5, 6].reduce((p, c) => Object.assign(p, {[c]: rolls.filter(x => x === c).length}), {})
  return (
    (Math.floor(rollsOfNum[1] / 3)) * 1000 +
    (Math.floor(rollsOfNum[6] / 3)) * 600 +
    (Math.floor(rollsOfNum[5] / 3)) * 500 +
    (Math.floor(rollsOfNum[4] / 3)) * 400 +
    (Math.floor(rollsOfNum[3] / 3)) * 300 +
    (Math.floor(rollsOfNum[2] / 3)) * 200 +
    (rollsOfNum[1] % 3) * 100 +
    (rollsOfNum[5] % 3) * 50
  );
};

const testRolls = [
  [5, 1, 3, 4, 1],
  [1, 1, 1, 3, 1],
  [2, 4, 4, 5, 4],
  [1, 1, 1, 1, 1, 1, 1, 1, 1],
  [3, 3, 3, 3, 2],
];
for (const testRoll of testRolls) {
  console.log(testRoll + ': ' + score(testRoll));
}