Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- const uniq = (arr)=>{
- return Array.from(new Set(arr));
- }
- let types = ['S', 'H', 'D', 'C'].reverse();
- let notNumFaces = ['J', 'Q', 'K', 'A'];
- const getScore = (str)=>{
- let type = str.substring(str.length-1);
- let face = str.substring(0,str.length-1);
- let faceScore = 0;
- if([74,75,65,81].includes(face[0].charCodeAt(0))){
- faceScore = notNumFaces.indexOf(face) + 11;
- }else{
- faceScore = Number(face);
- }
- let typeScore = types.indexOf(type)+1;
- return typeScore * faceScore;
- }
- let player = new Map();
- let arr = input.slice(0);
- for (const deal of arr) {
- let [name, handString] = deal.split(': ');
- let hands = uniq(handString.split(', '));
- if(!player.has(name)){
- player.set(name,hands);
- }else{
- let currentArray = player.get(name);
- currentArray.push(...hands);
- currentArray = uniq(currentArray);
- player.set(name, currentArray);
- }
- }
- for (const pair of Array.from(player)) {
- let name = pair[0];
- let cardArr = pair[1];
- let score = cardArr.map(card=>{
- return getScore(card);
- }).reduce((a,b)=>a+b);
- console.log(`${name}: ${score}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment