Advent of Code 2025 Day 1
I’ve heard about the Advent of Code before. I have been nervous to attempt it because I struggle with programming problems.
But now that I’ve graduated and have more time on my hands, I can’t find an excuse why I shouldn’t complete an attempt. This year will be much easier too because there are only 12 days rather than 25. I’ve decided to blog about the process so I can reflect on other people’s attempts and see how I can level up as a programmer.
I am also determined to go back and finish the other years too.
With that being said, let’s look at the Day 1 problem.
Part 1
def rotation(origin, rotation):
position = origin
dir, amount = rotation[0], int(rotation[1:])
if dir == "L":
position = position - amount
elif dir == "R":
position = position + amount
if position < 0 or position > 100:
position = position - math.floor(position / 100) * 100
if position == 100:
position = 0
return position
I’ve kept it simple here. I add the rotation amount to the original position and then subtract/add multiples of 100.
However, in my other attempts, I’ve noticed that my solution for Day 1 can’t be extended to Day 2. So I spend a few hours having to rethink my logic.
Part 2
This is my original attempt
def rotation(origin, rotation):
position, past_zero = origin, 0
dir, amount = rotation[0], int(rotation[1:])
if dir == "L":
position = position - amount
elif dir == "R":
position = position + amount
if position < 0 or position > 100:
past_zero = past_zero + abs(math.floor(position / 100))
position = position - math.floor(position / 100) * 100
if origin == 0:
past_zero = past_zero - 1
if position == 100 or position == 0:
past_zero = past_zero + 1
position = 0
return [position, past_zero]
So I think my problem is that I don’t try to rewrite the original code and then I’m struggling to work within the constraints I’ve set for myself.
That means I have two options: rethink the problem and tweak it OR commit to the hole that I’ve dug myself into.
Let’s look at both.
Bruteforcing. Rather than trying to think about the final position after each rotation, I can keep it simple and simulate each click. That will just require a simple if statement to see if I passed GO zero.
Then you get this solution.
def rotation(origin, rotation):
position, past_zero = origin, 0
dir, amount = rotation[0], int(rotation[1:])
for _ in range(amount):
if dir == "L":
position = (position - 1) % 100
else:
position = (position + 1) % 100
if position == 0:
past_zero = past_zero + 1
return [position, past_zero]
Whew! Sometimes I don’t have to work so hard.
Optimised Approach. There was some incorrect logic in my first version which gave a lower value. Sometimes, the syntax can obscure a straightforward solution. I made a few syntactic changes:
position - math.floor(position / 100) * 100is the equivalent ofposition % 100- Python has a floor division expression so
math.floor(position / 100)can be changed toposition // 100.
One method for calculating whether the zero boundary was crossed is by comparing the previous and final rotations. 1 needs to be subtracted as there can be overcounting while decrementing from 100 to 99. past_zero = (position - 1) // 100 - (position - amount - 1) // 100
This is the final solution:
def rotation(origin, rotation):
position, past_zero = origin, 0
dir, amount = rotation[0], int(rotation[1:])
if dir == "L":
past_zero = (position - 1) // 100 - (position - amount - 1) // 100
position = position - amount
else:
position = position + amount
past_zero = position // 100
return [position % 100, past_zero]
Final thoughts
My takeaway lessons were:
- Keep things simple. Bruteforcing is always an option.
- Rewrite Day 2 if you need to. Don’t think that you need to retain the Day 1 code.
- If the Day 2 solution isn’t easily solvable, then there was a problem with Day 1.
- I learnt about math expression statements that I don’t use commonly.