day 07
This commit is contained in:
parent
44973098cb
commit
0b40afdc6b
31
aoc_2023/day-07/example.test.ts
Normal file
31
aoc_2023/day-07/example.test.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { expect, test } from "bun:test";
|
||||||
|
import { main as part1, score } from "./part_1";
|
||||||
|
import { main as part2, score as scoreJoker } from "./part_2";
|
||||||
|
|
||||||
|
const example = `32T3K 765
|
||||||
|
T55J5 684
|
||||||
|
KK677 28
|
||||||
|
KTJJT 220
|
||||||
|
QQQJA 483`.split("\n");
|
||||||
|
|
||||||
|
test("part1", async () => {
|
||||||
|
const answer = 6440;
|
||||||
|
const res = await part1(example);
|
||||||
|
expect(res).toEqual(answer);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("matching", () => {
|
||||||
|
expect(score("AAAAA")).toEqual(6);
|
||||||
|
expect(score("AA8AA")).toEqual(5);
|
||||||
|
expect(score("23332")).toEqual(4);
|
||||||
|
expect(score("TTT98")).toEqual(3);
|
||||||
|
expect(score("23432")).toEqual(2);
|
||||||
|
expect(score("A23A4")).toEqual(1);
|
||||||
|
expect(score("23456")).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("part2", async () => {
|
||||||
|
const answer = 5905;
|
||||||
|
const res = await part2(example);
|
||||||
|
expect(res).toEqual(answer);
|
||||||
|
});
|
0
aoc_2023/day-07/logs/.gitkeep
Normal file
0
aoc_2023/day-07/logs/.gitkeep
Normal file
4010
aoc_2023/day-07/logs/out_1.txt
Normal file
4010
aoc_2023/day-07/logs/out_1.txt
Normal file
File diff suppressed because it is too large
Load Diff
4010
aoc_2023/day-07/logs/out_2.txt
Normal file
4010
aoc_2023/day-07/logs/out_2.txt
Normal file
File diff suppressed because it is too large
Load Diff
85
aoc_2023/day-07/part_1.ts
Normal file
85
aoc_2023/day-07/part_1.ts
Normal file
@ -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<number | string> => {
|
||||||
|
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);
|
||||||
|
}
|
98
aoc_2023/day-07/part_2.ts
Normal file
98
aoc_2023/day-07/part_2.ts
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
export const cardOrder = "A, K, Q, T, 9, 8, 7, 6, 5, 4, 3, 2, J"
|
||||||
|
.split(", ")
|
||||||
|
.reverse();
|
||||||
|
|
||||||
|
export const score = (hand: string) => {
|
||||||
|
const highestCardNotJoker = hand.split("").reduce(
|
||||||
|
([card, count], x) => {
|
||||||
|
if (x === "J") return [card, count] as [string, number];
|
||||||
|
const appearances = hand.match(new RegExp(x, "g"))!.length;
|
||||||
|
if (appearances > count) {
|
||||||
|
return [x, appearances] as [string, number];
|
||||||
|
}
|
||||||
|
return [card, count] as [string, number];
|
||||||
|
},
|
||||||
|
["J", 0] as [string, number]
|
||||||
|
);
|
||||||
|
const newHand = hand.replaceAll("J", highestCardNotJoker[0]);
|
||||||
|
const cards = newHand.split("");
|
||||||
|
|
||||||
|
// five of a kind
|
||||||
|
if (newHand.match(new RegExp(cards[0], "g"))?.length === cards.length)
|
||||||
|
return 6;
|
||||||
|
|
||||||
|
// four of a kind
|
||||||
|
for (const card of cards) {
|
||||||
|
if (newHand.match(new RegExp(card, "g"))?.length === 4) return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
// full house
|
||||||
|
let threeMatching = cards.some(
|
||||||
|
(card) => newHand.match(new RegExp(card, "g"))?.length === 3
|
||||||
|
);
|
||||||
|
let twoMatching = cards.find(
|
||||||
|
(card) => newHand.match(new RegExp(card, "g"))?.length === 2
|
||||||
|
);
|
||||||
|
if (threeMatching && twoMatching) return 4;
|
||||||
|
if (threeMatching) return 3;
|
||||||
|
|
||||||
|
let twoMatchingUnique = cards.find(
|
||||||
|
(card) =>
|
||||||
|
card !== twoMatching && newHand.match(new RegExp(card, "g"))?.length === 2
|
||||||
|
);
|
||||||
|
if (twoMatchingUnique) return 2;
|
||||||
|
|
||||||
|
if (twoMatching) return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const main = async (lines: string[]): Promise<number | string> => {
|
||||||
|
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 P2 ===");
|
||||||
|
console.log(answer);
|
||||||
|
}
|
1000
aoc_2023/day-07/problem.txt
Normal file
1000
aoc_2023/day-07/problem.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user