From 0b40afdc6be3aa1b4303c0b11f03fa1cae9bf110 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Wed, 6 Dec 2023 22:57:43 -0700 Subject: day 07 --- aoc_2023/day-07/part_1.ts | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 aoc_2023/day-07/part_1.ts (limited to 'aoc_2023/day-07/part_1.ts') diff --git a/aoc_2023/day-07/part_1.ts b/aoc_2023/day-07/part_1.ts new file mode 100644 index 0000000..3288873 --- /dev/null +++ b/aoc_2023/day-07/part_1.ts @@ -0,0 +1,85 @@ +export const cardOrder = "A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2" + .split(", ") + .reverse(); + +export const score = (hand: string) => { + const cards = hand.split(""); + + // five of a kind + if (hand.match(new RegExp(cards[0], "g"))?.length === cards.length) return 6; + + // four of a kind + for (const card of cards) { + if (hand.match(new RegExp(card, "g"))?.length === 4) return 5; + } + + // full house + let threeMatching = cards.some( + (card) => hand.match(new RegExp(card, "g"))?.length === 3 + ); + let twoMatching = cards.find( + (card) => hand.match(new RegExp(card, "g"))?.length === 2 + ); + if (threeMatching && twoMatching) return 4; + if (threeMatching) return 3; + + let twoMatchingUnique = cards.find( + (card) => + card !== twoMatching && hand.match(new RegExp(card, "g"))?.length === 2 + ); + if (twoMatchingUnique) return 2; + + if (twoMatching) return 1; + + return 0; +}; + +export const main = async (lines: string[]): Promise => { + const hands: { hand: string; bid: number }[] = lines + .map((line) => { + const [hand, bidS] = line.split(" "); + const bid = parseInt(bidS); + return { hand, bid }; + }) + .sort(({ hand: handOne }, { hand: handTwo }) => { + const scoreOne = score(handOne); + const scoreTwo = score(handTwo); + + if (scoreOne === scoreTwo) { + let [cardOneScore, cardTwoScore] = [-1, -1]; + let i = 0; + while (cardOneScore === cardTwoScore && i < handOne.length) { + cardOneScore = cardOrder.indexOf(handOne[i]); + cardTwoScore = cardOrder.indexOf(handTwo[i]); + i++; + } + + return cardOneScore - cardTwoScore; + } + return scoreOne - scoreTwo; + }); + console.log(hands); + + return hands.reduce((acc, { hand, bid }, i) => { + console.log(hand, bid, i + 1); + return acc + bid * (i + 1); + }, 0); +}; + +// + +const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path; +if (isrun) { + const file = Bun.file("./problem.txt"); + const text = await file.text(); + const lines = text.split("\n").filter((x) => x && x.length); + + console.log("=== COMPUTATION ===\n"); + + const answer = await main(lines); + + console.log("\n=== /COMPUTATION ===\n"); + + console.log("=== ANSWER TO P1 ==="); + console.log(answer); +} -- cgit v1.2.3-70-g09d2