|
| 1 | +const fs = require("node:fs"); |
| 2 | +const path = require("node:path"); |
| 3 | + |
| 4 | +const input = fs |
| 5 | + .readFileSync(path.join(__dirname, "../input.txt"), "utf8") |
| 6 | + .split("\n") |
| 7 | + .filter((line) => line !== ""); |
| 8 | + |
| 9 | +const RANKS = "0123456789TJQKA"; |
| 10 | + |
| 11 | +const getValue = (hand) => { |
| 12 | + if (fiveOfAKind(hand)) return 6; |
| 13 | + if (fourOfAKind(hand)) return 5; |
| 14 | + if (fullHouse(hand)) return 4; |
| 15 | + if (threeOfAKind(hand)) return 3; |
| 16 | + if (twoPair(hand)) return 2; |
| 17 | + if (onePair(hand)) return 1; |
| 18 | + return 0; |
| 19 | +}; |
| 20 | + |
| 21 | +const fiveOfAKind = (hand) => { |
| 22 | + const parsed = group(hand.split("")); |
| 23 | + return Object.values(parsed).some((rank) => rank.length == 5); |
| 24 | +}; |
| 25 | + |
| 26 | +const fourOfAKind = (hand) => { |
| 27 | + const parsed = group(hand.split("")); |
| 28 | + return Object.values(parsed).some((rank) => rank.length == 4); |
| 29 | +}; |
| 30 | + |
| 31 | +const fullHouse = (hand) => { |
| 32 | + const parsed = group(hand.split("")); |
| 33 | + return ( |
| 34 | + Object.values(parsed).some((rank) => rank.length == 3) && |
| 35 | + Object.values(parsed).some((rank) => rank.length == 2) |
| 36 | + ); |
| 37 | +}; |
| 38 | + |
| 39 | +const threeOfAKind = (hand) => { |
| 40 | + const parsed = group(hand.split("")); |
| 41 | + return Object.values(parsed).some((rank) => rank.length == 3); |
| 42 | +}; |
| 43 | + |
| 44 | +const twoPair = (hand) => { |
| 45 | + const parsed = group(hand.split("")); |
| 46 | + return Object.values(parsed).filter((value) => value.length == 2).length == 2; |
| 47 | +}; |
| 48 | + |
| 49 | +const onePair = (hand) => { |
| 50 | + const parsed = group(hand.split("")); |
| 51 | + return Object.values(parsed).filter((value) => value.length == 2).length == 1; |
| 52 | +}; |
| 53 | + |
| 54 | +const group = (arr) => |
| 55 | + arr.reduce((acc, cur) => { |
| 56 | + const firstLetter = cur[0].toLowerCase(); |
| 57 | + return { ...acc, [firstLetter]: [...(acc[firstLetter] || []), cur] }; |
| 58 | + }, {}); |
| 59 | + |
| 60 | +const part1 = (lines) => { |
| 61 | + const ranked = [[], [], [], [], [], [], []]; |
| 62 | + for (const line of lines) { |
| 63 | + const [hand, bidStr] = line.split(" "); |
| 64 | + |
| 65 | + ranked[getValue(hand)].push(line); |
| 66 | + } |
| 67 | + |
| 68 | + const sorted = ranked.map((rank) => |
| 69 | + rank.sort((a, b) => { |
| 70 | + for (let i = 0; i < a.length; i++) { |
| 71 | + if (RANKS.indexOf(a[i]) < RANKS.indexOf(b[i])) { |
| 72 | + return -1; |
| 73 | + } else if (RANKS.indexOf(a[i]) > RANKS.indexOf(b[i])) { |
| 74 | + return 1; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return 0; |
| 79 | + }) |
| 80 | + ); |
| 81 | + |
| 82 | + return sorted |
| 83 | + .flat() |
| 84 | + .map((hand, i) => { |
| 85 | + const [_, bidStr] = hand.split(" "); |
| 86 | + const bid = Number.parseInt(bidStr); |
| 87 | + return bid * (i + 1); // Index starts at 0 |
| 88 | + }) |
| 89 | + .reduce((acc, curr) => acc + curr, 0); |
| 90 | +}; |
| 91 | + |
| 92 | +console.log(part1(input)); |
0 commit comments