AoC 2025, Day 1 - Secret Entrance

Wednesday, December 3, 2025 at 3:02 AM

I enjoy coding. My current position is Software Engineer, so coding is something I don't do as often as I'd like. That's why I enjoy doing annual events like Advent of Code, a Christmas themed coding challenge held throughout the month of December.

AoC is a little different from years past. For one, it's 12 days this year, not 25. They have also removed the global leadership board. If you want to participate, check out more about the event here.

These posts will cover the day's puzzle and the process I took to discover the solution, as well as provide the code I used to solve it. With that considered, all solutions are spoilers to solving the puzzle proper. Don't read the solution below if you want the full experience of solving these puzzles yourself.

So now, we have Day 1: Secret Entrance.

Puzzle Summary

The elves need to figure out how to open a lock with 100 ticks, from 0 to 99. Each line in your puzzle input has a direction you move the dial (L for lower numbers on the left, R for higher numbers on the right) along with a number to indicate the total ticks to move. The starting position of the lock is 50. When you reach the limits at either end, you jump to the other limit, like an actual rotating lock. For example, if you have a line that says R65, you rotate to the right 65 ticks. Because you reach the end, you go back to the lower limit and continue counting ticks. You eventually reach tick 15. Same rules apply going left with a line like L33.

Part 1 of the puzzle is to find out how many times you land on the 0 tick.

Part 2 wants to know the sum total of how many times you pass the 0 tick.

General Strategy

Our input is converted into a JSON array object, where each line is an element:

["L33", "L2", "R88"]

We need to establish the type of arithmetic required when moving left/right. Left goes for smaller numbers, so we subtract, while adding on right numbers:

let pt = 50;
let dir = i.charAt(0),
  num = parseInt(i.substring(1)) * (dir == "L" ? -1 : 1);
pt = pt + num;
console.log(pt);

We get the first letter to determine direction, then parse the remaining string as an integer. Apply basic math, boom, you have its new position.

Part 1 Solution

First one isn't too bad, as it's just a modulus problem. Our lock is size 100, so after we change its position, we apply a modulo of 100 against our result to give us the true position on the lock:

let pt = 50,
  pwd1 = 0;
d.forEach((i) => {
  let dir = i.charAt(0),
    num = parseInt(i.substring(1)) * (dir == "L" ? -1 : 1);
  pt = (pt + num) % lockSize;
  if (pt == 0) {
    pwd1++;
  }
});

When our position is at 0, increase our password counter. At the end of the array, you have your password answer.

Part 2 Solution

This one required a little thinking. We have to determine how many rotations a single direction command completes. When we apply our addition/subtraction, we get the new position before our modulo function:

pt += num;

If we have a result beyond our range limits, we can divide our result by the lock size of 100, transform it into an absolute value using Math.abs, and round the result up with a Math.ceil:

if (pt < 0 || pt > lockSize) {
  pwd2 += Math.ceil(Math.abs(pt / lockSize));
}

This sum result of pwd2 should be the solution to part 2, but it's not. The result is too high. Through some Reddit help threads, it's double counting when it reaches a tick point. So, back to the drawing board.

Maybe we could calculate a difference between old positions and new ones? We already know the lock size is 100. If the difference between old and new is at or above 100, we know it hit a tick:

oldPt = pt;
pt = pt + num;
let oldSpins = Math.abs(
    oldPt <= 0 ? Math.ceil(oldPt / s) : Math.floor(oldPt / s),
  ),
  newSpins = Math.abs(pt <= 0 ? Math.ceil(pt / s) : Math.floor(pt / s));
console.log(`${oldSpins} - ${newSpins}`);
pwd2 += Math.abs(oldSpins - newSpins);

Run our answer through the site, and it's still incorrect. Hm.

Do we incorporate the for loop method? Or maybe the while loop?

Let's do some while loops:

oldPt = pt;
pt = pt + num;
if (oldPt < pt) {
  while (oldPt < pt) {
    oldPt++;
    if (oldPt % s == 0) {
      pwd2++;
    }
  }
} else {
  while (oldPt > pt) {
    oldPt--;
    if (oldPt % s == 0) {
      pwd2++;
    }
  }
}

We add/subtract on our oldPt until we reach the current pt, checking for 0 ticks on the way.

Submit, and success!

Code Improvements

Our answer is correct, but eh, the code looks messy. We can clean it up.

Create a function that takes in our old/new values as well as an object with a tick property:

const DetermineDirection = (o, n, i) => {
  i.tick = o < n ? 1 : -1;
  return o != n;
};

We need to use an object to insure we can pass by reference in Javascript land, so that our while loop will now move in the proper direction:

let j = { tick: 0 };
while (DetermineDirection(oldPt, pt, j)) {
  oldPt += j.tick;
  if (oldPt % s == 0) pwd2++;
}

No duplicate code, same result. Just how I like it.

Final Thoughts and Code

Good primer puzzle. A little more difficult compared to other first day puzzles, but that's alright.

My final code:

var data1 = require("./day01.json");
//data1 = ["L68", "L30", "R48", "L5", "R60", "L55", "L1", "L99", "R14", "L82"];
var lockSize = 100;

const DetermineDirection = (o, n, i) => {
  i.tick = o < n ? 1 : -1;
  return o != n;
};

var Day1A = (d, s) => {
  let pt = 50,
    oldPt = 50,
    pwd1 = 0,
    pwd2 = 0;
  d.forEach((i) => {
    let dir = i.charAt(0),
      num = parseInt(i.substring(1)) * (dir == "L" ? -1 : 1);
    oldPt = pt;
    pt = pt + num;
    let j = { tick: 0 };
    while (DetermineDirection(oldPt, pt, j)) {
      oldPt += j.tick;
      if (oldPt % s == 0) pwd2++;
    }
    if (pt % s == 0) {
      pwd1++;
    }
  });
  return `${pwd1} - ${pwd2}`;
};

console.log(Day1A(data1, lockSize));