Day Seven Part Two

This commit is contained in:
Lewis Dale 2023-12-07 13:09:13 +00:00
parent ce50531971
commit 3105abc935
1 changed files with 20 additions and 2 deletions

View File

@ -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<string, number>;
private hand: Record<string, number>;
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<string, number>;
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;
}
}