|
@@ -2,108 +2,129 @@ import java.util.*;
|
2
|
2
|
import java.util.stream.Collectors;
|
3
|
3
|
|
4
|
4
|
class Hand {
|
5
|
|
- private String input;
|
6
|
|
- private int score;
|
|
5
|
+ private String input;
|
|
6
|
+ private int score;
|
7
|
7
|
|
8
|
|
- Hand(String hand) {
|
9
|
|
- this.input = hand;
|
10
|
|
- this.score = scoreHand(parseCards(hand));
|
11
|
|
- }
|
|
8
|
+ Hand(String hand) {
|
|
9
|
+ this.input = hand;
|
|
10
|
+ this.score = scoreHand(parseCards(hand));
|
|
11
|
+ }
|
12
|
12
|
|
13
|
|
- int getScore() {
|
14
|
|
- return score;
|
15
|
|
- }
|
|
13
|
+ int getScore() {
|
|
14
|
+ return score;
|
|
15
|
+ }
|
16
|
16
|
|
17
|
|
- String getInput() {
|
18
|
|
- return input;
|
19
|
|
- }
|
|
17
|
+ String getInput() {
|
|
18
|
+ return input;
|
|
19
|
+ }
|
20
|
20
|
|
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
|
|
- }
|
|
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
|
+ }
|
29
|
29
|
|
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
|
|
- }
|
|
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
|
+ }
|
41
|
41
|
|
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());
|
|
42
|
+ private int scoreHand(List<Card> cards) {
|
|
43
|
+ List<Card> cardsByRank = cards.stream().sorted(Comparator.comparing(Card::getRank)).unordered()
|
|
44
|
+ .collect(Collectors.toList());
|
48
|
45
|
|
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());
|
|
46
|
+ Map<Integer, Integer> frequencyMap = getFrequencyMap(cards);
|
|
47
|
+ List<Integer> ranks = frequencyMap.entrySet().stream().map(Map.Entry::getKey).sorted(Comparator.reverseOrder())
|
|
48
|
+ .collect(Collectors.toList());
|
|
49
|
+ frequencyMap = frequencyMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
|
|
50
|
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
|
51
|
+ List<Integer> rankCounts = frequencyMap.entrySet().stream().map(Map.Entry::getValue)
|
|
52
|
+ .collect(Collectors.toList());
|
|
53
|
+ List<Integer> suits = cards.stream().map(Card::getSuit).collect(Collectors.toList());
|
67
|
54
|
|
68
|
|
- return calculatedScore(frequencyMap, cardsByRank, ranks, rankCounts, suits);
|
69
|
|
- }
|
|
55
|
+ return calculatedScore(frequencyMap, cardsByRank, ranks, rankCounts, suits);
|
|
56
|
+ }
|
70
|
57
|
|
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
|
|
- }
|
|
58
|
+ private int calculatedScore(Map<Integer, Integer> frequencyMap, List<Card> cardsByRank, List<Integer> ranks,
|
|
59
|
+ List<Integer> rankCounts, List<Integer> suits) {
|
|
60
|
+ if (ranks.equals(Arrays.asList(14, 5, 4, 3, 2))) {
|
|
61
|
+ ranks = Arrays.asList(5, 4, 3, 2, 1);
|
|
62
|
+ }
|
76
|
63
|
|
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();
|
|
64
|
+ boolean flush = suits.stream().distinct().count() == 1;
|
|
65
|
+ boolean straight = ranks.stream().distinct().count() == 5 && ranks.get(0) - ranks.get(4) == 4;
|
|
66
|
+ Iterator<Integer> iteratorOverFrequencies = frequencyMap.keySet().iterator();
|
|
67
|
+ int highestFrequency = iteratorOverFrequencies.next();
|
|
68
|
+
|
|
69
|
+ int maxValue = Collections.max(ranks);
|
|
70
|
+
|
|
71
|
+ if (straight && flush) {
|
|
72
|
+ return 800 + highestFrequency;
|
|
73
|
+ }
|
|
74
|
+ if (rankCounts.equals(Arrays.asList(4, 1))) {
|
|
75
|
+ return 700 + cardsByRank.get(0).getRank();
|
|
76
|
+ }
|
|
77
|
+ if (rankCounts.equals(Arrays.asList(3, 2))) {
|
|
78
|
+ int triplet = 0;
|
|
79
|
+ int pair = 0;
|
|
80
|
+ for (Object key : frequencyMap.keySet()) {
|
|
81
|
+ if (frequencyMap.get(key) == 2) {
|
|
82
|
+ pair = (int) key;
|
|
83
|
+ }
|
|
84
|
+ if (frequencyMap.get(key) == 3) {
|
|
85
|
+ triplet = 3 * (int) key;
|
|
86
|
+ }
|
|
87
|
+ }
|
|
88
|
+ return 600 + 3 * triplet + pair;
|
|
89
|
+ }
|
|
90
|
+ if (flush) {
|
|
91
|
+ return 500 + highestFrequency;
|
|
92
|
+ }
|
|
93
|
+ if (straight) {
|
|
94
|
+ return 400 + maxValue;
|
|
95
|
+ }
|
|
96
|
+ if (rankCounts.equals(Arrays.asList(3, 1, 1))) {
|
|
97
|
+ List<Integer> cardsf1 = new ArrayList<Integer>();
|
|
98
|
+ int triplet = 0;
|
|
99
|
+ for (Object key : frequencyMap.keySet()) {
|
|
100
|
+ if (frequencyMap.get(key) == 1) {
|
|
101
|
+ cardsf1.add((int) key);
|
|
102
|
+ }
|
|
103
|
+ if (frequencyMap.get(key) == 3) {
|
|
104
|
+ triplet = 3 * (int) key;
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ return 300 + triplet + Collections.max(cardsf1);
|
|
109
|
+ }
|
|
110
|
+ if (rankCounts.equals(Arrays.asList(2, 2, 1))) {
|
|
111
|
+ int sum = 0;
|
|
112
|
+ for (Object key : frequencyMap.keySet()) {
|
|
113
|
+ int fKey = (int) key;
|
|
114
|
+ int fValue = frequencyMap.get(key);
|
|
115
|
+ sum += fKey * fValue;
|
|
116
|
+ }
|
|
117
|
+ return 200 + sum + 2 * Math.max(highestFrequency, iteratorOverFrequencies.next());
|
|
118
|
+ }
|
|
119
|
+ if (rankCounts.equals(Arrays.asList(2, 1, 1, 1))) {
|
|
120
|
+ return 100 + highestFrequency;
|
|
121
|
+ }
|
|
122
|
+ ranks.sort(Comparator.naturalOrder());
|
|
123
|
+ int result = 0;
|
|
124
|
+ for (int i = 0; i < ranks.size(); i++) {
|
|
125
|
+ result += ranks.get(0) * (i + 1);
|
|
126
|
+ }
|
|
127
|
+ return result + ranks.get(ranks.size() - 1);
|
|
128
|
+ }
|
81
|
129
|
|
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
|
130
|
}
|