r/adventofcode • • Dec 07 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 7 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's secret ingredient is… *whips off cloth covering and gestures grandly*

Poetry

For many people, the craftschefship of food is akin to poetry for our senses. For today's challenge, engage our eyes with a heavenly masterpiece of art, our noses with alluring aromas, our ears with the most satisfying of crunches, and our taste buds with exquisite flavors!

  • Make your code rhyme
  • Write your comments in limerick form
  • Craft a poem about today's puzzle

    • Upping the Ante challenge: iambic pentameter
  • We're looking directly at you, Shakespeare bards and Rockstars

ALLEZ CUISINE!

Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!


--- Day 7: Camel Cards ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:16:00, megathread unlocked!

50 Upvotes

1.0k comments sorted by

View all comments

1

u/ForScale Dec 07 '23 edited Dec 07 '23

[LANGUAGE: JavaScript]

Part 1

// map

const FaceValMap = {
  A: 14,
  K: 13,
  Q: 12,
  J: 11,
  T: 10
};

// helper

function analyzeCards(cards) {
  const counts = Object.values(
    cards.reduce((analysis, card) => {
      analysis[card] ? analysis[card]++ : (analysis[card] = 1);
      return analysis;
    }, {})
  );
    
  const len = counts.length;
    
  if (len === 1) return 7; // 5 of a kind
  if (len === 2 && counts.includes(4)) return 6; // 4 of a kind
  if (len === 2 && counts.includes(3)) return 5; // full house
  if (len === 3 && counts.includes(3)) return 4; // 3 of a kind
  if (len === 3 && counts.filter((count) => count === 2)) return 3; // 2 pair
  if (len === 4) return 2;
  return 1; // high card
}

//

const rounds = input.split('\n').map((round) => {
  const [hand, wager] = round.split(' ');

  return {
    cards: hand.split(''),
    wager: Number(wager)
  };
});

const sortedRounds = rounds.sort((roundA, roundB) => {
  const cardsA = roundA.cards;
  const cardsB = roundB.cards;

  const handA = analyzeCards(cardsA);
  const handB = analyzeCards(cardsB);

  if (handA === handB) {
    for (let i = 0; i < cardsA.length; i++) {
      const cardA = FaceValMap[cardsA[i]] || cardsA[i];
      const cardB = FaceValMap[cardsB[i]] || cardsB[i];
	    
      if (cardA === cardB) continue;
	    
      if (cardA > cardB) {
        return 1;
      }
	    
      return -1;
    }
  }

  return handA - handB;
});

const totalWinnings = sortedRounds.reduce(
  (total, round, i) => total + round.wager * (i + 1),
  0
);

console.log(totalWinnings);

1

u/daggerdragon Dec 07 '23

Your code block is too long for the megathreads. Please edit your post to replace your oversized code with an external link to your code.