finalized d7p1

This commit is contained in:
Baipyrus 2023-12-08 22:33:04 +01:00
parent ceacaf6948
commit e3eeec7fd9

View File

@ -15,27 +15,43 @@ 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((p) => { const plays = input
const [draw, bidding] = p.split(" "); .map((p) => {
const [draw, bidding] = p.split(" ");
const count = draw const count = draw
.split("") .split("")
.map((e) => cards.indexOf(e)) .map((e) => cards.indexOf(e))
.sort((a, b) => b - a) .sort((a, b) => b - a)
.reduce((a, i) => { .reduce((a, i) => {
const t = a; const t = a;
t[i] = (t[i] ?? 0) + 1; t[i] = (t[i] ?? 0) + 1;
return t; return t;
}, {}); }, {});
const pattern = Object.keys(count) const pattern = Object.keys(count)
.map((e) => count[e]) .map((e) => count[e])
.sort((a, b) => b - a); .sort((a, b) => b - a);
const hand = cardPattern.findIndex((a) => const hand = cardPattern.findIndex((a) =>
a.every((b, c) => b === pattern[c]) a.every((b, c) => b === pattern[c])
); );
return { hand, draw, bidding: parseInt(bidding) }; return { hand, draw, bidding: parseInt(bidding) };
}); })
.sort((a, b) => {
const type = a.hand - b.hand;
if (type !== 0) return type;
for (const [i, c] of a.draw.split("").entries()) {
const p1 = cards.indexOf(c);
const p2 = cards.indexOf(b.draw[i]);
if (p1 === p2) continue;
return p1 > p2 ? -1 : 1;
}
return 0;
})
.map((e, i) => e.bidding * (i + 1))
.reduce((a, i) => a + i);
console.log(plays); console.log(plays);
const t1 = performance.now(); const t1 = performance.now();