LogoWCF

dallinstevens's Solution

2025 Solution Deno


import { open } from 'node:fs/promises';

if (import.meta.main) {
  const file = await open("assembly_codes.txt");

  let preValidCounter = 0;
  let postValidCounter = 0;
  let totalAdds = 0;
  let totalRemoves = 0;

  for await (const line of file.readLines()) {
    const [valid] = validateCode(line);
    if (valid) {
      preValidCounter++;
    }

    const [validEndingCode, fixedLine] = fixCode(line);

    if (!validEndingCode) {
      continue;
    }

    const [validAfter, adds, removes] = validateCode(fixedLine);
    if (validAfter) {
      postValidCounter++;
    }
    totalAdds += adds;
    totalRemoves += removes;
  }
  console.log(`Total valid codes to begin with: ${preValidCounter}`);
  console.log(`Total fixed codes: ${postValidCounter - preValidCounter}`);
  console.log(`Total Adds from valid codes: ${totalAdds}`);
  console.log(`Total Removes from valid codes: ${totalRemoves}`);
}

function fixCode(line: string): [boolean, string] { // Returns whether the ending code is valid and the fixed line
  if (line.slice(-1) !== 'P' && line.slice(-2) !== 'C') {
    return [false, line];
  }
  return [true, line.split("").slice(0, -2).join("").replaceAll('C', 'A').replaceAll('P', 'R') + 'CP'];
}

function validateCode(line: string): [boolean, number, number] { // Returns if the code is valid, number of valid adds and number of valid removes
  const lineArray = line.split('');
  const lastChar = lineArray.pop();
  const secondToLastChar = lineArray.pop();

  let adds = 0;
  let removes = 0;

  if (lastChar !== 'P') {
    return [false, 0, 0]; // Don't report counts for invalid codes
  }

  if (secondToLastChar !== 'C') {
    return [false, 0, 0]; // Don't report counts for invalid codes
  }

  let addCounter = 0;
  for (let i = 0; i < lineArray.length; i++) {
    if (line[i] === 'C' || line[i] === 'P') {
      return [false, 0, 0]; // Don't report counts for invalid codes
    }

    if (line[i] === 'A') {
      addCounter++;
      adds++;
    }
    if (line[i] === 'R') {
      if (addCounter === 0) {
        return [false, 0, 0]; // Don't report counts for invalid codes
      }
      removes++;
      addCounter--;
    }
  }
  return [true, adds, removes];
}

Day 3: Elf Assembly Line

Part 1

On a particular day at the North Pole, the elves are running an assembly line packaging presents. Each present is assigned a code upon completion that contains a sequence of instructions for how to assemble it. However, there seems to be a problem with the printer, and now, many presents have invalid sequences!

For each present, its assembly code consists of a string with the following commands:

An assembly code is considered valid if there is a sequence of A and R commands leading to a correctly assembled present, followed by a single C, and ending in P. If there are multiple C or P commands, then the code is invalid, as the elves should only check and package it once. Also, there cannot be an R without a corresponding A before it (as the elves can’t remove a part they haven’t added).

For example, the assembly code AACP is valid (add two parts, check, and then package), while RCP (removal without corresponding addition) and ACCP (two checks) is not.

Problem: Your task is to write a function that receives a list from assembly_codes.txt and returns the number of valid codes.

Example input:

ACP
CRARACP
ARAAACP
ARAPCP
AARRACP
ACR
ARCAP
ARACP
ARACP
ARCP

Example output (number of valid codes):

6

The elves want to get to the bottom of this and to start they need to know how many assembly codes are still valid. Count how many are complete and ready to be packaged.

Part 2

After a while, and thanks to you for getting how many presents have a correct code, the elves figure out what is wrong with the printer. The assembly commands are getting mixed up when printing. The A and C commands sometimes get swapped and the R and the P commands can get swapped. So codes that have a C command anywhere else but as the second to last command are actually A commands, and codes that have a P command anywhere else but at the end are actually a R code.

The elves want to know how many parts were added and removed in total. After fixing the assembly codes count all the parts added and removed and report that back to the elves.