maymunskoa

05. Card Game (Assoc Arrays)

Mar 7th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.     const uniq = (arr)=>{
  3.         return Array.from(new Set(arr));
  4.     }
  5.  
  6.    
  7.     let types = ['S', 'H', 'D', 'C'].reverse();
  8.     let notNumFaces = ['J', 'Q', 'K', 'A'];
  9.    
  10.  
  11.  
  12.     const getScore = (str)=>{
  13.        
  14.         let type = str.substring(str.length-1);
  15.         let face = str.substring(0,str.length-1);
  16.         let faceScore = 0;
  17.         if([74,75,65,81].includes(face[0].charCodeAt(0))){
  18.          faceScore = notNumFaces.indexOf(face) + 11;
  19.         }else{
  20.             faceScore = Number(face);
  21.         }
  22.         let typeScore = types.indexOf(type)+1;  
  23.         return typeScore * faceScore;
  24.     }
  25.  
  26.  
  27.     let player = new Map();
  28.     let arr = input.slice(0);
  29.  
  30.     for (const deal of arr) {
  31.         let [name, handString] = deal.split(': ');
  32.         let hands = uniq(handString.split(', '));
  33.        
  34.  
  35.         if(!player.has(name)){
  36.             player.set(name,hands);
  37.         }else{
  38.             let currentArray = player.get(name);
  39.             currentArray.push(...hands);
  40.             currentArray = uniq(currentArray);
  41.             player.set(name, currentArray);
  42.         }
  43.  
  44.     }
  45.  
  46.     for (const pair of Array.from(player)) {
  47.         let name = pair[0];
  48.         let cardArr = pair[1];
  49.         let score = cardArr.map(card=>{
  50.             return getScore(card);
  51.         }).reduce((a,b)=>a+b);
  52.  
  53.         console.log(`${name}: ${score}`);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment