Advent of Code 2025 - In Swift!
It's that time of the year when software engineers blow the dust off of their data structures and algorithms books! My humble attempt at some of this year's challenges, in Swift, with some explanations. Beware, there are spoilers. Updated as I pick up the challenges.
š” You can find all of my solutions at github.com/GSebastian/advent-of-code-2025.
Day 1
Part 1
An intuitive problem of keeping track of a current value through additions (moves to the right e.g. R10) and subtractions (moves to the left e.g. L23).
Take notice - the dial starts at 50, not at 0 as I'd defaulted to while skimming the problem.
The complication: values under 0 go back to 99 (e.g -1 is actually 99) and similarly for cases over 99.
A key thing to realise here is that 50 - 51 is the same as 50 - 151, same as 50 - 251 etc, so we can modulo by 100 to simplify things when building an intuition. It also means that, if we modulo the rotation value by 100, the total result of adding the current position and the rotation cannot exceed 99 (maximum current position) + 99 (maximum rotation if modulo'd by 100).
From here, there are 2 approaches.
"Branch-y" approach
"Branch-y" because it relies on an if statement.
We keep track of the current value, by current = current + (sign * rotation % 100). sign is of course 1 or -1 for right and left rotations respectively.
Then,
pos = if total > 99 {
total % 100
} else if total < 0 {
100 - abs(total)
} else {
total
}pos = if total > 99 {
total % 100
} else if total < 0 {
100 - abs(total)
} else {
total
}If pos is 0, then we have a valid rotation, so we add it to the total count.
"Clean" approach
The "clean" approach doesn't use any branching at all, and simply uses a formula.
Here's some intuition around it:
- If
pos == 50. Add 300.350 % 100== 50 (correct, and already discussed above). - If
pos == 50. Subtract 170.-120 % 100 == -20.- But this should be 80.
- If we add 100 - now we have 80, which is the correct result.
-120 % 100and350 % 100are common to both cases. But adding 100 is only in the negative case. So how can we have a uniform formula? Remember how we said that50 - 51is the same as50 - 151?We can cover the cases where the total dips below 0 by adding 100, then doing a modulo 100 operation on the result.
All of this leads us to the following formula:
pos = (100 + (total % 100)) % 100 where total = pos + sign * rotation
Much nicer!
Part 2
Here, we need to keep track of whether we've crossed the 0 position, even if we haven't landed on it.
I couldn't come up with a single-formula solution for this, so the solution is built on top of the "branch-y" one above.
- Firstly, we keep track how many times the rotation itself goes through zero -
R300is guaranteed to go through zero 3 times, regardless of what the current position was. We can do this by dividing by 100:(sign * rotation) / 100. We store this. - Then, we check if adding the value above to the current position puts us through zero again.
These 2 things take us to an accurate count. I won't expand further on my particular implementation, purely because I think it's more convoluted than it should be!
Days 2-5
I haven't done a write-up for these days yet, but please do have a look at github.com/GSebastian/advent-of-code-2025 for the solutions.
Ā© 2025 Sebastian Ghetu. All rights reserved.
