Compare commits
No commits in common. "ec23232f15d93cd6f63c55971f6176d789a917aa" and "595ffe2a494b52be22799737ab5fc3a327427203" have entirely different histories.
ec23232f15
...
595ffe2a49
|
@ -1,8 +1,5 @@
|
||||||
{
|
{
|
||||||
"deno.enable": true,
|
"deno.enable": true,
|
||||||
"deno.lint": true,
|
"deno.lint": true,
|
||||||
"deno.unstable": false,
|
"deno.unstable": false
|
||||||
"deno.enablePaths": [
|
|
||||||
"./day3"
|
|
||||||
]
|
|
||||||
}
|
}
|
|
@ -31,9 +31,4 @@ I'll probably try to vary languages a bit to get some variety.
|
||||||
**Note**: Here begin the optimisations! (some people got away with brute force, I decided not to even go there).
|
**Note**: Here begin the optimisations! (some people got away with brute force, I decided not to even go there).
|
||||||
|
|
||||||
### [Day 6](day6/)
|
### [Day 6](day6/)
|
||||||
**Language**: Haskell
|
**Language**: Haskell
|
||||||
|
|
||||||
### [Day 7](day7/)
|
|
||||||
**Language**: JavaScript (Node)
|
|
||||||
|
|
||||||
**Note**: Yes, I used JSDoc for type-hints. But I think only insane people use JavaScript without JSDoc.
|
|
|
@ -1,5 +0,0 @@
|
||||||
32T3K 765
|
|
||||||
T55J5 684
|
|
||||||
KK677 28
|
|
||||||
KTJJT 220
|
|
||||||
QQQJA 483
|
|
1000
day7/input.txt
1000
day7/input.txt
File diff suppressed because it is too large
Load Diff
|
@ -1,85 +0,0 @@
|
||||||
const fs = require("fs/promises");
|
|
||||||
|
|
||||||
const order = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"];
|
|
||||||
const newOrder = ["A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", "2", "J"];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string[]} hand
|
|
||||||
* @param {boolean} useJoker
|
|
||||||
*/
|
|
||||||
function handType(hand, useJoker = false) {
|
|
||||||
function eq(v) {
|
|
||||||
return c => c === v || useJoker && c === "J";
|
|
||||||
}
|
|
||||||
const s = new Set(useJoker ? hand.filter(c => c !== "J") : hand);
|
|
||||||
if (s.size === 1 || s.size === 0) { // 0 if JJJJJ and useJoker == true
|
|
||||||
return 0;
|
|
||||||
} else if (s.size === 2) {
|
|
||||||
let m = -1;
|
|
||||||
for (const v of s.values()) {
|
|
||||||
const count = hand.filter(eq(v)).length;
|
|
||||||
if (count > m) m = count;
|
|
||||||
}
|
|
||||||
if (m === 4) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 2;
|
|
||||||
} else if (s.size === 3) {
|
|
||||||
let m = -1;
|
|
||||||
for (const v of s.values()) {
|
|
||||||
const count = hand.filter(eq(v)).length;
|
|
||||||
if (count > m) m = count;
|
|
||||||
}
|
|
||||||
if (m === 3) {
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
return 4;
|
|
||||||
} else if (s.size === 4) {
|
|
||||||
return 5;
|
|
||||||
}
|
|
||||||
return 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string[]} h1
|
|
||||||
* @param {string[]} h2
|
|
||||||
* @param {boolean} useJoker
|
|
||||||
*/
|
|
||||||
function cmpHands(h1, h2, useJoker = false) {
|
|
||||||
const [t1, t2] = [handType(h1, useJoker), handType(h2, useJoker)];
|
|
||||||
if (t1 !== t2) return t1 - t2;
|
|
||||||
for (let idx = 0; idx < h1.length; idx++) {
|
|
||||||
const [v1, v2] = [h1[idx], h2[idx]];
|
|
||||||
if (v1 !== v2) {
|
|
||||||
return useJoker ? newOrder.indexOf(v1) - newOrder.indexOf(v2)
|
|
||||||
: order.indexOf(v1) - order.indexOf(v2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {{hand: string[], bid: number}[]} hands
|
|
||||||
*/
|
|
||||||
function totalWinnings(hands) {
|
|
||||||
return hands.map((h, idx) => h.bid * (idx + 1))
|
|
||||||
.reduce((p, c) => p + c)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const lines = await fs.readFile("input.txt", {encoding: "utf8"})
|
|
||||||
.then(s => s.split("\n"));
|
|
||||||
const hands = lines.map(l => {
|
|
||||||
const [h, b] = l.split(" ");
|
|
||||||
return {
|
|
||||||
hand: h.split(""),
|
|
||||||
bid: Number(b)
|
|
||||||
};
|
|
||||||
});
|
|
||||||
hands.sort((h1, h2) => cmpHands(h2.hand, h1.hand));
|
|
||||||
console.log("Part 1:", totalWinnings(hands));
|
|
||||||
hands.sort((h1, h2) => cmpHands(h2.hand, h1.hand, true));
|
|
||||||
console.log("Part 2:", totalWinnings(hands));
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
Loading…
Reference in New Issue