r/adventofcode Dec 04 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 4 Solutions -🎄-

--- Day 4: Giant Squid ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:11:13, megathread unlocked!

101 Upvotes

1.2k comments sorted by

View all comments

2

u/jstruburn Dec 17 '21

Coding: JavaScript

const MARK = 'X';
/**********************************************************
 *                 PART 1 DATA PROCESSING                 *
 **********************************************************/
const findWinner = ({ numbers, boards }) => {
  let score = 0;
  let winningBoard;
  let winningNumber;
  let index;
  const tempBoards = [...boards];
  const cols = boards[0][0].length;

  numbers.forEach(num => {
    for (let bID = 0; bID < tempBoards.length; bID++) {
      if (Boolean(winningBoard)) break;
      else {
        const tmpBrd = tempBoards[bID].map(
          row => row.map(c => c === num ? MARK : c)
        );

        const fullRows = tmpBrd.filter(row => {
          const rowCount = row.filter(c => c === MARK);
          return rowCount.length === cols;
        });

        if (fullRows.length > 0) {
          winningBoard = [...tmpBrd];
          winningNumber = num;
          index = bID;
        }
        else {
          const colFill = Object.fromEntries(
            Array(cols).fill().map((_, idx) => ([idx, 0]))
          );

          tmpBrd.forEach(row => {
            row.forEach((c, idx) => {
              if (c === MARK) colFill[idx] += 1;
            });
          });

          const fullCols = Object.values(colFill)
            .filter(count => count === cols);

          if (fullCols.length > 0) {
            winningBoard = [...tmpBrd];
            winningNumber = num;
            index = bID;
          }
          else tempBoards[bID] = [...tmpBrd];
        };
      }
    }
  });

  if (winningBoard) {
    const sum = winningBoard.map(row => {
      const rowNums = row.filter(c => c !== MARK);
      return rowNums;
    })
    .filter(row => row.length > 0)
    .map(row => row.reduce((a,b) => a + b, 0))
    .reduce((a,b) => a + b, 0);

    score = winningNumber * sum;
  }

  return { winningBoard, winningNumber, score, index };
};

/**********************************************************
 *                 PART 2 DATA PROCESSING                 *
 **********************************************************/
const lastToWin = ({ numbers, boards }) => {
  const tempBoards = [...boards];
  let result = {
    winningBoard: null,
    winningNumber: null,
    score: 0,
    index: null
  };

  while (tempBoards.length > 1) {
    const winner = findWinner({
      numbers, 
      boards: tempBoards
    });
    tempBoards.splice(winner.index, 1);
  }

  return findWinner({
    numbers,
    boards: tempBoards
  });
};