Day 6: Trash Compactor

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 2 points 10 months ago

(Browser-based) Javascript

I got lazy and lucky writing the first part; hard-coded the number of lines / operands and it worked on the first try! I didn't factor in the final block not being padded by spaces on both sides for part 2, and so needed to test on the example code โ€“ which has 3 lines of operands instead of the problem input's 4 lines, so I had to properly solve the problem across all possible amounts of lines of input ๐Ÿ˜ฉ.

I am very jealous of all y'all who have a transpose function available in your language of choice.

Code

function part1(inputText) {
  const [firstArgs, secondArgs, thirdArgs, fourthArgs, operators] = inputText.trim().split('\n').map(line => line.trim().split(/\s+/));
  let totalSum = 0;
  for (let i = 0; i < firstArgs.length; i++) {
    if (operators[i] === '+') {
      totalSum += Number.parseInt(firstArgs[i], 10) + Number.parseInt(secondArgs[i], 10) + Number.parseInt(thirdArgs[i], 10) + Number.parseInt(fourthArgs[i], 10);
    } else if (operators[i] === '*') {
      totalSum += Number.parseInt(firstArgs[i], 10) * Number.parseInt(secondArgs[i], 10) * Number.parseInt(thirdArgs[i], 10) * Number.parseInt(fourthArgs[i], 10);
    }
  }
  return totalSum
}

{
  const start = performance.now();
  const result = part1(document.body.textContent);
  const end = performance.now();
  console.info({
    day: 6,
    part: 1,
    time: end - start,
    result
  })
}

function part2(inputText) {
  const lines = inputText.trimEnd().split('\n');
  const interBlockIndices = [];
  for (let i = 0; i < lines[0].length; i++) {
    let allEmpty = true;
    for (let j = 0; j < lines.length; j++) {
      if (lines[j][i] !== ' ') {
        allEmpty = false;
        break;
      }
    }
    if (allEmpty) {
      interBlockIndices.push(i);
    }
  }
  interBlockIndices.push(lines[0].length);

  let totalSum = 0;
  let blockStart = 0;
  for (const interBlockIndex of interBlockIndices) {
    // compute calculation of block [blockStart, interBlockIndex - 1]
    const operands = [];
    for (let i = interBlockIndex - 1; i >= blockStart; i--) {
      // parse operands
      let operand = 0;
      for (let j = 0; j < lines.length - 1; j++) {
        if (lines[j][i] !== ' ') {
          operand *= 10;
          operand += Number.parseInt(lines[j][i], 10);
        }
      }
      operands.push(operand)
    }
    if (lines.at(-1)[blockStart] === '+') {
      totalSum += operands.reduce((accu, next) => accu + next, 0);
    } else if (lines.at(-1)[blockStart] === '*') {
      totalSum += operands.reduce((accu, next) => accu * next, 1);
    }
    //  console.debug({ totalSum, operands, blockStart, interBlockIndex });
    blockStart = interBlockIndex + 1;
  }
  return totalSum;
}

{
  const example = `123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +  
`;
  const start = performance.now();
  const result = part2(document.body.textContent);
  //   const result = part2(example);
  const end = performance.now();
  console.info({
    day: 6,
    part: 2,
    time: end - start,
    result
  })
}

  • source