mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-11-12 17:13:48 +00:00
get exact hand type
This commit is contained in:
parent
2fcefde8f7
commit
ceacaf6948
@ -1,32 +1,40 @@
|
||||
import { readFileSync } from "fs";
|
||||
const t0 = performance.now();
|
||||
|
||||
const patterns = [
|
||||
"01234",
|
||||
"00123",
|
||||
"00112",
|
||||
"00012",
|
||||
"00011",
|
||||
"00001",
|
||||
"00000",
|
||||
const cardPattern = [
|
||||
[1, 1, 1, 1, 1],
|
||||
[2, 1, 1, 1],
|
||||
[2, 2, 1],
|
||||
[3, 1, 1],
|
||||
[3, 2],
|
||||
[4, 1],
|
||||
[5],
|
||||
];
|
||||
const cards = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"];
|
||||
const input = readFileSync("input.txt")
|
||||
.toString()
|
||||
.split("\r\n")
|
||||
.filter((e) => e.length > 0);
|
||||
const plays = input.map((e) => {
|
||||
const [draw, bidding] = e.split(" ");
|
||||
const plays = input.map((p) => {
|
||||
const [draw, bidding] = p.split(" ");
|
||||
|
||||
let count = 0;
|
||||
const hand = draw
|
||||
const count = draw
|
||||
.split("")
|
||||
.map((e) => cards.indexOf(e))
|
||||
.sort((a, b) => b - a)
|
||||
.map((e, i, a) => (i !== 0 && e !== (a[i - 1] ?? 0) ? ++count : count))
|
||||
.reduce((a, i) => a + i.toString());
|
||||
.reduce((a, i) => {
|
||||
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user