From ceacaf6948f00949388bb72941e0df4c82334b6c Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 8 Dec 2023 22:21:34 +0100 Subject: [PATCH] get exact hand type --- Day7/Part1.js | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/Day7/Part1.js b/Day7/Part1.js index 3e2f490..9cee6d1 100644 --- a/Day7/Part1.js +++ b/Day7/Part1.js @@ -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);