-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
181 lines (164 loc) · 4.16 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const fs = require("fs");
// Parse input.txt to an array of arrays
let content = fs.readFileSync("input.txt", "utf-8").split("\n");
content.forEach(function(line, index) {
content[index] = line.split(" ");
});
// define cards and styles
const cardsLow = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"T",
"J",
"Q",
"K"
];
const cardsHigh = [
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"T",
"J",
"Q",
"K",
"A"
];
// card sets for testing purposes
const straightFlush = ["2H", "5H", "AH", "3H", "4H"];
const quads = ["QC", "QD", "QS", "QH", "KH"];
// Calculate best possible hand
function bestPossibleHand(array) {
const hand = array.slice(0, 5);
const deck = array.slice(5, 10);
let possibleHands = arrayCombination(hand);
possibleHands.forEach((possibleHand, index) => {
const tempHand = possibleHand;
const tempDeck = deck.slice(0, 5 - possibleHand.length);
tempDeck.forEach(item => {
tempHand.push(item);
});
if (checkFlush(tempHand) && checkStraight(tempHand)) {
possibleHands[index] = 20;
} else if (checkFlush(tempHand)) {
possibleHands[index] = 12.5;
} else if (checkStraight(tempHand)) {
possibleHands[index] = 11.5;
} else {
possibleHands[index] = checkHand(tempHand).reduce(
(sum, accumulator) => sum + accumulator
);
}
});
const handScore = Math.max.apply(null, possibleHands);
switch (handScore) {
case 5:
return { bestPossibleHand: "highest-card", hand: hand, deck: deck };
case 7:
return { bestPossibleHand: "one-pair", hand: hand, deck: deck };
case 9:
return { bestPossibleHand: "two-pairs", hand: hand, deck: deck };
case 11:
return { bestPossibleHand: "three-of-a-kind", hand: hand, deck: deck };
case 11.5:
return { bestPossibleHand: "straight", hand: hand, deck: deck };
case 12.5:
return { bestPossibleHand: "flush", hand: hand, deck: deck };
case 13:
return { bestPossibleHand: "full-house", hand: hand, deck: deck };
case 17:
return { bestPossibleHand: "four-of-a-kind", hand: hand, deck: deck };
case 20:
return { bestPossibleHand: "straight-flush", hand: hand, deck: deck };
}
}
//Check if an array has consecutive items
function isConsecutive(array) {
let returnArray = [];
let result = true;
array
.sort((a, b) => a > b)
.forEach((value, index) => {
if (array[index + 1]) {
returnArray[index] = array[index + 1] - value === 1;
} else {
returnArray[index] = value - array[index - 1] === 1;
}
if (returnArray[index] === false) {
result = false;
}
});
return result;
}
//Check if an array of cards makes a flush
function checkFlush(array) {
return array.every((val, i, array) => val[1] === array[0][1]);
}
//Check if an array of cards makes a straight
function checkStraight(array) {
let handLow = [];
let handHigh = [];
let result;
array.forEach(function(card, index) {
handHigh[index] = cardsHigh.indexOf(card[0]);
handLow[index] = cardsLow.indexOf(card[0]);
});
return isConsecutive(handHigh) || isConsecutive(handLow);
}
//Checks hand and returns hand structure/# of repeating cards
function checkHand(array) {
let hand = [];
array.forEach((value, index) => {
let counter = 0;
array.forEach(newValue => {
if (value[0] === newValue[0]) {
counter++;
}
});
hand[index] = counter;
});
return hand.sort((a, b) => a < b);
}
//returns all combinations of the array items
function arrayCombination(arr) {
let i, j, temp;
let result = [];
let arrLen = arr.length;
let power = Math.pow;
let combinations = power(2, arrLen);
// Time & Space Complexity O (n * 2^n)
for (i = 0; i < combinations; i++) {
temp = [];
for (j = 0; j < arrLen; j++) {
// & is bitwise AND
if (i & power(2, j)) {
temp.push(arr[j]);
}
}
result.push(temp);
}
return result;
}
content.forEach(line => {
let result = bestPossibleHand(line);
console.log(
"Hand: " +
result.hand +
" Deck: " +
result.deck +
" Best hand: " +
result.bestPossibleHand
);
// console.log(result)
});