matthe.me

Advent of Code 2025 - My Approach to Day 1

Introduction

Welcome to my first blog post! I wanted to share my approach on the coding puzzles of the Advent of Code event as a series of blog posts. This is the first one! Yesterday, the existence of this project suddenly popped up in my head after not having thought about it for some time.

I was introduced to the event two years ago by a programming teacher in college. He encouraged our class to try the puzzles to help gain experience with programming. Together with some fellow students, I took on the challenge and completed the first seven days. At that time, the project still had 24 puzzles that were released one at a time each day, from 1 till 24th of December. Since this year, only 12 puzzles will be released, as making them is quite time-intensive.

My approach on the first puzzle

Each puzzle consists of two parts. Submitting a correct answer for the first one unlocks the second one. The first day is about acquiring a secret password to open a safe. This safe has a dial with numbers 0 through 99 around it. The puzzle input is a list of rotation instructions:

1L25
2L8
3L44
4L6
5R1
6R26
7R9
8...

Each instruction consists of a direction (R for right, L for left) and a number indicating how far to rotate the dial in that direction.

Part 1

The first part required counting how many times the dial ended up exactly in the 0 position. In my solution, I made use of the % (modulo) operator in Python, which yields the remainder from the division of the first argument by the second (10 % 3 = 1). I used this operator with a different functionality in mind (namely that of the c modulus operator) so I was suprised to learn it always produces a positive result. This came in handy, as I did not have to convert the negative value to a valid dial position manually. For example, if the dial starts at position 10 and it is rotated left by 20, we arrive at a value of -10. To convert this number to a valid dial number, we simply use this modulo operator: -10 % 100 = 90. Here is my final solution:

 1dial = 50
 2count = 0
 3
 4with open("input") as file:
 5    lines = file.readlines()
 6    for line in lines:
 7        line = line.strip()
 8        incr = line[0] == "R"
 9        change = int(line[1:])
10        new_dial = dial + change if incr else dial - change
11        if dial % 100 == 0:
12            count += 1
13        dial = new_dial
14
15print(count)

We start by reading the file line by line, and determining the direction of rotation. The incr variable indicates whether we increase (True) or decrease (False). We then extract the number into the change variable and calculate the new dial position. Using the method explained earlier, we then calculate whether the dial lands at the 0 position. If so, the count is incremented. Finally, we print the count.