AoC-23/Day7/Part1.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-12-08 10:49:03 +00:00
import { readFileSync } from "fs";
const t0 = performance.now();
2023-12-08 21:21:34 +00:00
const cardPattern = [
[1, 1, 1, 1, 1],
[2, 1, 1, 1],
[2, 2, 1],
[3, 1, 1],
[3, 2],
[4, 1],
[5],
2023-12-08 19:34:34 +00:00
];
2023-12-08 21:02:30 +00:00
const cards = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"];
const input = readFileSync("input.txt")
.toString()
2023-12-09 17:53:39 +00:00
.split("\n")
2023-12-08 21:02:30 +00:00
.filter((e) => e.length > 0);
2023-12-08 21:33:04 +00:00
const plays = input
.map((p) => {
const [draw, bidding] = p.split(" ");
2023-12-08 21:02:30 +00:00
2023-12-08 21:33:04 +00:00
const count = draw
.split("")
.map((e) => cards.indexOf(e))
.sort((a, b) => b - a)
.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])
);
2023-12-08 21:02:30 +00:00
2023-12-08 21:33:04 +00:00
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);
2023-12-08 21:02:30 +00:00
console.log(plays);
2023-12-08 10:49:03 +00:00
const t1 = performance.now();
console.log(`Runtime: ${t1 - t0}ms`);