From 3105abc935d1c2adac232e293086e47ee1548b4e Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Thu, 7 Dec 2023 13:09:13 +0000 Subject: [PATCH] Day Seven Part Two --- src/day_seven.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/day_seven.ts b/src/day_seven.ts index 3b5ece5..151f916 100644 --- a/src/day_seven.ts +++ b/src/day_seven.ts @@ -1,4 +1,4 @@ -import {isEqual, zip} from "lodash"; +import {isEqual, omit, zip} from "lodash"; import {anyChar, int, newline, rest, space, whitespace} from "parjs"; import {stringify, manyBetween, between, then, manySepBy, manyTill} from "parjs/combinators"; import fs from "fs"; @@ -6,7 +6,7 @@ import fs from "fs"; const CardLetterScores = ['J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A']; export class CamelCard { - private readonly hand: Record; + private hand: Record; public readonly score: number; constructor(protected readonly card: string) { @@ -35,7 +35,24 @@ export class CamelCard { return 0; } + private redistributeJ(): void { + if ('J' in this.hand) { + const js = this.hand.J; + const withoutJ = omit(this.hand, 'J') as Record; + + const [mostCommon, mostCommonValue] = Object.entries(withoutJ).reduce(([maxKey, maxValue], [key, value]) => { + if (value > maxValue) return [key, value]; + return [maxKey, maxValue]; + }, ['J', 0]); + + withoutJ[mostCommon] = mostCommonValue + js; + this.hand = withoutJ; + } + } + private calculateScore(): number { + this.redistributeJ(); + const cards = Object.values(this.hand).sort((a, b) => b-a); if (isEqual(cards, [1, 1, 1, 1, 1])) return 1; @@ -46,6 +63,7 @@ export class CamelCard { if (isEqual(cards, [4, 1])) return 6; if (isEqual(cards, [5])) return 7; + console.log(`Returning 0 for ${JSON.stringify(this.hand)}`) return 0; } }