1- import java .util .ArrayList ;
2- import java .util .Arrays ;
3- import java .util .Collections ;
4- import java .util .Comparator ;
5- import java .util .HashMap ;
6- import java .util .Iterator ;
7- import java .util .LinkedHashMap ;
8- import java .util .List ;
9- import java .util .Map ;
1+ import java .util .*;
102import java .util .stream .Collectors ;
113
12- public class Hand {
13- private String input ;
14- private int score ;
4+ class Hand {
5+ private String input ;
6+ private int score ;
157
16- public Hand (String hand ) {
17- this .input = hand ;
18- this .score = scoreHand (parseCards (hand ));
19- }
8+ Hand (String hand ) {
9+ this .input = hand ;
10+ this .score = scoreHand (parseCards (hand ));
11+ }
2012
21- public int getScore () {
22- return score ;
23- }
13+ int getScore () {
14+ return score ;
15+ }
2416
25- public String getInput () {
26- return input ;
27- }
17+ String getInput () {
18+ return input ;
19+ }
2820
29- private List <Card > parseCards (String hand ) {
30- ArrayList <Card > parsedCards = new ArrayList <Card >();
31- String [] parseString = hand .split (" " );
32- for ( int i = 0 ; i < parseString . length ; i ++ ) {
33- parsedCards .add (new Card (parseString [ i ] ));
34- }
35- return parsedCards ;
36- }
21+ private List <Card > parseCards (String hand ) {
22+ ArrayList <Card > parsedCards = new ArrayList <>();
23+ String [] cardsToParse = hand .split (" " );
24+ for ( String cardToParse : cardsToParse ) {
25+ parsedCards .add (new Card (cardToParse ));
26+ }
27+ return parsedCards ;
28+ }
3729
38- private Map <Integer , Integer > getfrequencyMap (List <Card > cards ) {
39- Map <Integer , Integer > frequencyMap = new HashMap <Integer , Integer >();
40- for (Card c : cards ) {
41- if (frequencyMap .containsKey (c .getRank ())) {
42- frequencyMap .put (c .getRank (), frequencyMap .get (c .getRank ()) + 1 );
43- } else {
44- frequencyMap .put (c .getRank (), 1 );
45- }
46- }
47- return frequencyMap ;
48- }
30+ private Map <Integer , Integer > getFrequencyMap (List <Card > cards ) {
31+ Map <Integer , Integer > frequencyMap = new HashMap <>();
32+ for (Card c : cards ) {
33+ if (frequencyMap .containsKey (c .getRank ())) {
34+ frequencyMap .put (c .getRank (), frequencyMap .get (c .getRank ()) + 1 );
35+ } else {
36+ frequencyMap .put (c .getRank (), 1 );
37+ }
38+ }
39+ return frequencyMap ;
40+ }
4941
50- private int scoreHand (List <Card > cards ) {
51- List <Card > cardsByRank = cards .stream ().sorted (Comparator .comparing (Card ::getRank )).unordered ()
52- .collect (Collectors .toList ());
42+ private int scoreHand (List <Card > cards ) {
43+ List <Card > cardsByRank = cards
44+ .stream ()
45+ .sorted (Comparator .comparing (Card ::getRank ))
46+ .unordered ()
47+ .collect (Collectors .toList ());
5348
54- Map <Integer , Integer > frequencyMap = getfrequencyMap (cards );
55- List <Integer > ranks = frequencyMap .entrySet ().stream ().map (x -> x .getKey ()).sorted (Comparator .reverseOrder ())
56- .collect (Collectors .toList ());
57- frequencyMap = frequencyMap .entrySet ().stream ().sorted (Map .Entry .comparingByValue (Collections .reverseOrder ()))
58- .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue , (e1 , e2 ) -> e1 , LinkedHashMap ::new ));
59- List <Integer > rankCounts = frequencyMap .entrySet ().stream ().map (x -> x .getValue ()).collect (Collectors .toList ());
60- List <Integer > suits = cards .stream ().map (s -> s .getSuit ()).collect (Collectors .toList ());
49+ Map <Integer , Integer > frequencyMap = getFrequencyMap (cards );
50+ List <Integer > ranks = frequencyMap
51+ .entrySet ()
52+ .stream ()
53+ .map (Map .Entry ::getKey )
54+ .sorted (Comparator .reverseOrder ())
55+ .collect (Collectors .toList ());
56+ frequencyMap = frequencyMap
57+ .entrySet ()
58+ .stream ()
59+ .sorted (Map .Entry .comparingByValue (Collections .reverseOrder ()))
60+ .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue , (e1 , e2 ) -> e1 , LinkedHashMap ::new ));
61+ List <Integer > rankCounts = frequencyMap
62+ .entrySet ()
63+ .stream ()
64+ .map (Map .Entry ::getValue )
65+ .collect (Collectors .toList ());
66+ List <Integer > suits = cards .stream ().map (Card ::getSuit ).collect (Collectors .toList ());
6167
62- return calculatedScore (frequencyMap , cardsByRank , ranks , rankCounts , suits );
63- }
68+ return calculatedScore (frequencyMap , cardsByRank , ranks , rankCounts , suits );
69+ }
6470
65- private int calculatedScore (Map <Integer , Integer > frequencyMap , List <Card > cardsByRank , List <Integer > ranks ,
66- List <Integer > rankCounts , List <Integer > suits ) {
67- if (ranks .equals (Arrays .asList (14 , 5 , 4 , 3 , 2 ))) {
68- ranks = Arrays .asList (5 , 4 , 3 , 2 , 1 );
69- }
71+ private int calculatedScore (Map <Integer , Integer > frequencyMap , List <Card > cardsByRank , List <Integer > ranks ,
72+ List <Integer > rankCounts , List <Integer > suits ) {
73+ if (ranks .equals (Arrays .asList (14 , 5 , 4 , 3 , 2 ))) {
74+ ranks = Arrays .asList (5 , 4 , 3 , 2 , 1 );
75+ }
7076
71- boolean flush = suits .stream ().distinct ().count () == 1 ;
72- boolean straight = ranks .stream ().distinct ().count () == 5 && ranks .get (0 ) - ranks .get (4 ) == 4 ;
73- Iterator <Integer > iteratorOverFrequencies = frequencyMap .keySet ().iterator ();
74- int highestFrequency = iteratorOverFrequencies .next ();
77+ boolean flush = suits .stream ().distinct ().count () == 1 ;
78+ boolean straight = ranks .stream ().distinct ().count () == 5 && ranks .get (0 ) - ranks .get (4 ) == 4 ;
79+ Iterator <Integer > iteratorOverFrequencies = frequencyMap .keySet ().iterator ();
80+ int highestFrequency = iteratorOverFrequencies .next ();
7581
76- if (straight && flush ) {
77- return 800 + highestFrequency ;
78- }
79- if (rankCounts .equals (Arrays .asList (4 , 1 ))) {
80- return 700 + cardsByRank .get (0 ).getRank ();
81- }
82- if (rankCounts .equals (Arrays .asList (3 , 2 ))) {
83- return 600 + cardsByRank .get (0 ).getRank ();
84- }
85- if (flush ) {
86- return 500 + highestFrequency ;
87- }
88- if (straight ) {
89- return 400 + highestFrequency ;
90- }
91- if (rankCounts .equals (Arrays .asList (3 , 1 , 1 ))) {
92- return 300 + cardsByRank .get (0 ).getRank ();
93- }
94- if (rankCounts .equals (Arrays .asList (2 , 2 , 1 ))) {
95- return 200 + Math .max (highestFrequency , iteratorOverFrequencies .next ());
96- }
97- if (rankCounts .equals (Arrays .asList (2 , 1 , 1 , 1 ))) {
98- return 100 + highestFrequency ;
99- }
100- Collections .sort (ranks , (a , b ) -> b .compareTo (a ));
101- Collections .reverse (ranks );
102- return ranks .get (0 );
103- }
104- }
82+ if (straight && flush ) {
83+ return 800 + highestFrequency ;
84+ }
85+ if (rankCounts .equals (Arrays .asList (4 , 1 ))) {
86+ return 700 + cardsByRank .get (0 ).getRank ();
87+ }
88+ if (rankCounts .equals (Arrays .asList (3 , 2 ))) {
89+ return 600 + cardsByRank .get (0 ).getRank ();
90+ }
91+ if (flush ) {
92+ return 500 + highestFrequency ;
93+ }
94+ if (straight ) {
95+ return 400 + highestFrequency ;
96+ }
97+ if (rankCounts .equals (Arrays .asList (3 , 1 , 1 ))) {
98+ return 300 + cardsByRank .get (0 ).getRank ();
99+ }
100+ if (rankCounts .equals (Arrays .asList (2 , 2 , 1 ))) {
101+ return 200 + Math .max (highestFrequency , iteratorOverFrequencies .next ());
102+ }
103+ if (rankCounts .equals (Arrays .asList (2 , 1 , 1 , 1 ))) {
104+ return 100 + highestFrequency ;
105+ }
106+ ranks .sort (Comparator .naturalOrder ());
107+ return ranks .get (0 );
108+ }
109+ }
0 commit comments