← blog

Twelve Days · day 1 · both parts

Day 1: Secret Entrance

the puzzle ↗ #aoc-2025#python

A warm-up: part 1 is a running sum and a %. Part 2 is the same dial with one more question, and the interesting part is answering it without turning the dial one click at a time.

The puzzle, in my words

A safe’s dial shows 0 to 99 and starts at 50. Each line of the input turns it left (down) or right (up) by some number of clicks, wrapping round: one click left from 0 is 99. Count how often the dial ends a rotation on 0.

In the example, L68 L30 R48 L5 R60 L55 L1 L99 R14 L82, the dial stops at 82, 52, 0, 95, 55, 0, 99, 0, 14 and 32: three zeros.

Part 1: where each rotation stops

Part 1 on the example. Right: each rotation and where it stops; red marks the three that stop on 0.

Step 1: the first version

Follow the dial: add or subtract the clicks, then bring the number back into 0 to 99 by hand.

def part1(text: str) -> int:
    dial = 50
    zeros = 0
    for line in text.split():
        direction, clicks = line[0], int(line[1:])
        if direction == "R":
            dial = dial + clicks
        else:
            dial = dial - clicks
        while dial < 0:
            dial += 100
        while dial > 99:
            dial -= 100
        if dial == 0:
            zeros += 1
    return zeros

Step 2: % does the wrapping

In Python, x % 100 is always between 0 and 99 when the modulus is positive, even for a negative x: -18 % 100 is 82, where C or Java would give -18. So both while loops become one %. Reading each rotation as a signed number (right positive, left negative) removes the if, and adding a comparison counts it, since True is 1:

def turns(text: str) -> list[int]:
    return [int(line[1:]) * (1 if line[0] == "R" else -1) for line in text.split()]


def part1(text: str) -> int:
    dial, zeros = 50, 0
    for turn in turns(text):
        dial = (dial + turn) % 100
        zeros += dial == 0
    return zeros

Step 3: the positions are a running sum

Without the wrapping, the dial’s position after each rotation is just 50 plus the turns so far: a running sum, which is what itertools.accumulate yields. A position is on 0 exactly when it’s a multiple of 100, so the wrap can wait until the end:

from itertools import accumulate


def part1(text: str) -> int:
    return sum(pos % 100 == 0 for pos in accumulate(turns(text), initial=50))

initial=50 (Python 3.8+) puts the starting position first, which is never 0 here, so it doesn’t change the count.

Part 2: every click that lands on 0

Now a pass counts too: any click that leaves the dial on 0, in the middle of a rotation or at its end. On the example that adds one pass each in L68, R60 and L82, so 6 in all. A single R1000 from 50 would count 10.

Part 2 on the same rotations. A red mark now means the pointer reached 0 at some point in that rotation.

Step 1: click by click

The rule is about clicks, so turn the dial one click at a time:

def part2(text: str) -> int:
    dial, zeros = 50, 0
    for turn in turns(text):
        step = 1 if turn > 0 else -1
        for _ in range(abs(turn)):
            dial = (dial + step) % 100
            zeros += dial == 0
    return zeros

The loop runs once per click, not once per line. On a generated input of 4,500 rotations of up to 999 clicks each, that’s about 100 ms. Fast enough, but there’s a formula.

Step 2: count the zeros instead of visiting them

Turning right from position p, the first 0 comes after 100 - p clicks (after a full 100 if p is already 0), and then one every 100 clicks. So d clicks reach 0 exactly (p + d) // 100 times. That covers p = 0 too: d // 100 full laps.

Turning left is the same dial seen in a mirror: position p becomes (100 - p) % 100, and the same formula applies.

def part2(text: str) -> int:
    dial, zeros = 50, 0
    for turn in turns(text):
        from_zero = dial if turn > 0 else (100 - dial) % 100
        zeros += (from_zero + abs(turn)) // 100
        dial = (dial + turn) % 100
    return zeros

Check it on the example’s first line: L68 from 50 mirrors to 50, and (50 + 68) // 100 = 1, the pass on the way to 82. On the same generated input this takes about 1 ms instead of 100.

The % 100 on the mirrored position is the part that is easy to get wrong: from 0, turning left must not count the 0 the dial starts on, and (100 - 0) % 100 = 0 makes sure it doesn’t. The tests run both versions on 500 random sequences, including turns of exactly 100 and 200 and turns that start from 0, and check that they agree.

All of this was checked on the puzzle’s examples and on generated inputs, not on a puzzle input of my own.