this post was submitted on 04 Dec 2025
16 points (100.0% liked)

Advent Of Code

1199 readers
3 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 4: Printing Department

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
[โ€“] Jayjader@jlai.lu 1 points 1 week ago

(Browser-based) Javascript

This was a good opportunity to refresh my grasp on the math involved in losslessly stuffing a tuple into a single number. JS-in-the-browser has Sets and Maps but no Tuples, and Arrays are indexed on their id / memory handle instead of their value contents, so if you want to put coordinates into a set or map and have the collection behave as expected you need to serialize the coordinates into a primitive type. Stuff it into a string if you don't want to think too hard. For this specific problem we don't even need to be able to compute the original coordinates (just count the unique removed points) but implementing that computation was a handy way to verify the "serializer" was working correctly.

Seeing as the record tuple proposal was withdrawn in February of this year this is still a technique worth knowing when working with coords in JS.

Code

function part1(inputText) {
  const gridWidth = inputText.indexOf('\n');
  const lines = inputText.trim().split('\n');
  const gridHeight = lines.length;
  let accessibleRolls = 0;
  for (let y = 0; y < gridHeight; y++) {
    for (let x = 0; x < gridWidth; x++) {
      if (lines[y][x] === '@') {
        let occupiedNeighbors = 0;
        for (const [neighborX, neighborY] of [
            [x - 1, y],
            [x + 1, y],
            [x, y - 1],
            [x, y + 1],
            [x - 1, y - 1],
            [x - 1, y + 1],
            [x + 1, y - 1],
            [x + 1, y + 1],
          ]) {
          if (neighborX < 0 || neighborX >= gridWidth || neighborY < 0 || neighborY >= gridHeight) {
            continue;
          }
          if (lines[neighborY][neighborX] === '@') {
            occupiedNeighbors++;
          }
        }
        if (occupiedNeighbors < 4) {
          accessibleRolls++;
        }
      }
    }
  }
  return accessibleRolls;
}
{
  const start = performance.now()
  const result = part1(document.body.textContent)
  const end = performance.now()
  console.info({day: 4, part: 1, time: end - start, result})
}

function serializeCoords(x, y, gridWidth) {
  const leftShiftAmount = Math.ceil(Math.log10(gridWidth));
  return x * (10 ** leftShiftAmount) + y;
}
/*
{
  const x = 3;
  const y = 4;
  const gridWidth = 13;
  const serialized = serializeCoords(x, y, gridWidth);
  console.debug({ x, y, gridWidth, serialized });
}
*/

function deserializeCoords(serialized, gridWidth) {
  const leftShiftAmount = Math.ceil(Math.log10(gridWidth));
  const x = Math.floor(serialized / (10 ** leftShiftAmount));
  const y = serialized - x * 10 ** leftShiftAmount;
  return [x, y];
}
/*
{
  const serialized = 304;
  const gridWidth = 13;
  const [x, y] = deserializeCoords(serialized, gridWidth);
  console.debug({ serialized, gridWidth, x, y });
}
*/
function part2(inputText) {
  const gridWidth = inputText.indexOf('\n');
  const lines = inputText.trim().split('\n');
  const gridHeight = lines.length;
  let removed = new Set();
  while (true) {
    const toRemove = new Set();
    for (let y = 0; y < gridHeight; y++) {
      for (let x = 0; x < gridWidth; x++) {
        const serialized = serializeCoords(x, y, gridWidth);
        if (lines[y][x] === '@' && !removed.has(serialized)) {
          let occupiedNeighbors = 0;
          for (const [neighborX, neighborY] of [
              [x - 1, y],
              [x + 1, y],
              [x, y - 1],
              [x, y + 1],
              [x - 1, y - 1],
              [x - 1, y + 1],
              [x + 1, y - 1],
              [x + 1, y + 1],
            ]) {
            if (neighborX < 0 || neighborX >= gridWidth || neighborY < 0 || neighborY >= gridHeight) {
              continue;
            }
            const serializedNeighbor = serializeCoords(neighborX, neighborY, gridWidth);
            if (lines[neighborY][neighborX] === '@' && !removed.has(serializedNeighbor)) {
              occupiedNeighbors++;
            }
          }
          if (occupiedNeighbors < 4) {
            toRemove.add(serialized);
          }
        }
      }
    }
    if (toRemove.size === 0) {
      break;
    }
    removed = removed.union(toRemove);
  }
  return removed.size;
}
/*
{
  const exampleText = `..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
`;
  const start = performance.now();
  const result = part2(exampleText);
  const end = performance.now();
  console.info({ day: 4, part: 2, time: end - start, result });
}
*/

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