get exact hand type

This commit is contained in:
Baipyrus 2023-12-08 22:21:34 +01:00
parent 2fcefde8f7
commit ceacaf6948

View File

@ -1,32 +1,40 @@
import { readFileSync } from "fs"; import { readFileSync } from "fs";
const t0 = performance.now(); const t0 = performance.now();
const patterns = [ const cardPattern = [
"01234", [1, 1, 1, 1, 1],
"00123", [2, 1, 1, 1],
"00112", [2, 2, 1],
"00012", [3, 1, 1],
"00011", [3, 2],
"00001", [4, 1],
"00000", [5],
]; ];
const cards = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"]; const cards = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"];
const input = readFileSync("input.txt") const input = readFileSync("input.txt")
.toString() .toString()
.split("\r\n") .split("\r\n")
.filter((e) => e.length > 0); .filter((e) => e.length > 0);
const plays = input.map((e) => { const plays = input.map((p) => {
const [draw, bidding] = e.split(" "); const [draw, bidding] = p.split(" ");
let count = 0; const count = draw
const hand = draw
.split("") .split("")
.map((e) => cards.indexOf(e)) .map((e) => cards.indexOf(e))
.sort((a, b) => b - a) .sort((a, b) => b - a)
.map((e, i, a) => (i !== 0 && e !== (a[i - 1] ?? 0) ? ++count : count)) .reduce((a, i) => {
.reduce((a, i) => a + i.toString()); const t = a;
t[i] = (t[i] ?? 0) + 1;
return t;
}, {});
const pattern = Object.keys(count)
.map((e) => count[e])
.sort((a, b) => b - a);
const hand = cardPattern.findIndex((a) =>
a.every((b, c) => b === pattern[c])
);
return { hand, draw, bidding }; return { hand, draw, bidding: parseInt(bidding) };
}); });
console.log(plays); console.log(plays);