Score a hand of Cribbage!

2

1

Goal:

Given a hand of 5 cards, determine its value in the game Cribbage.

Input a string through any reasonable means. You can assume the string will always be in this format:

[Card Value][Card Face],[Card Value][Card Face],[Card Value][Card Face],[Card Value][Card Face],[Card Value][Card Face]

Valid Card Values:

A,2,3,4,5,6,7,8,9,10,J,Q,K

Valid Card Faces:

H,D,S,C

Examples:

5H,6D,9C,KC,JH
AH,AD,AS,AC,2H

Rules:

Each item in the list is a card. Score the cards based on the following rules:

- Any card with the same value as another is worth 2 points. 

- A "run" of at least 3 cards, that is, cards that have sequential values are worth the same amount of points as there are cards. (A run of 4 cards is worth 4 points). Aces are worth 1, and JQK are all worth 10.

- Cards whose values add up to 15 or 31 are worth 2 points. You may use any amount of cards for this.

- You may use any card over and over to form different combinations. If you have three cards of the value, you may count 3 separate pairs. However, if you have 4 cards forming a run, you may not count 2 runs of 3, only a run of 4.

- The output of the program is the sum of the points. 

Examples

Input: "2C,10D,10H,AD,9C"
// 10D and 10H is a pair -> 2 points 
// 2C, 9C, 10D, and 10H sum to 31 -> 2 points
Output: 4


Input: "10D,AH,JS,9H,QC"
// 9H, 10D, JS, and QC is a run of 4 -> 4 points
Output: 4


Input: "AH,AD,AS,AC,3C"
// AH and AD, AH and AS, AH and AC, AD and AS, AD and AC, and AS and AC are all pairs -> 12 points. 
Output: 12

Julian Lachniet

Posted 2016-12-28T14:49:12.003

Reputation: 3 216

Question was closed 2016-12-28T16:23:54.693

This is an interesting challenge, but I'm afraid it's already been done before. – James – 2016-12-28T16:24:18.827

How did you not see that listed under "possible duplicates" when you typed in the question? – Rɪᴋᴇʀ – 2016-12-28T17:43:02.337

1I love the game and can understand why this is a great challenge. Unfortunately it is most definitely a dupe as I looked at doing the same. Why don't you post it in the sandbox and maybe we can work on a variation which is not a dupe and provides an interesting challenge? I have a couple of ideas. – ElPedro – 2016-12-28T18:01:31.693

and by the way, you missed out 1 for the Jack if it is the same suit as the turnover card and also points for the flush if all are the same suit (must be 5 if in the box/crib) :-) – ElPedro – 2016-12-28T18:09:36.170

and "Cards whose values add up to 15 or 31 are worth 2 points" is not strictly correct. 31 only counts when you are pegging, not in the hand. – ElPedro – 2016-12-28T18:15:23.493

1@DJMcMayhem I previosly wrote the entire game in Java then ported to Javascript. Would be interested in seeing what golfing languages can really do with this. Would you support a variation on the theme after some time in the sandbox? – ElPedro – 2016-12-28T18:26:32.653

No answers