summaryrefslogtreecommitdiff
path: root/aoc_2023/day-07/part_1.ts
blob: 74975a47d6ae061b0096bcfb5aba84dc5585d9c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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;
    });

  return hands.reduce((acc, { bid }, i) => 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);
}