AoC-23/Day7/Part1.js

43 lines
921 B
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()
.split("\r\n")
.filter((e) => e.length > 0);
2023-12-08 21:21:34 +00:00
const plays = input.map((p) => {
const [draw, bidding] = p.split(" ");
2023-12-08 21:02:30 +00:00
2023-12-08 21:21:34 +00:00
const count = draw
2023-12-08 21:02:30 +00:00
.split("")
.map((e) => cards.indexOf(e))
.sort((a, b) => b - a)
2023-12-08 21:21:34 +00:00
.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:21:34 +00:00
return { hand, draw, bidding: parseInt(bidding) };
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`);