2025-advent-of-code

following along with the advent of code 2025 (Matlab)

View on GitHub

Day 02

So - this turns out to be a bit more tricky that I first thought. The problem sounds like it should be ok to solve.

  1. You have to parse the file into the pairs. You can do this with textread() in matlab by specifying the delimiter as , - and you will end up with a piece of text that contains the ranges of IDs. E.g. 11-30 or 12344-14999 or smilar

  2. Splitting on the - will give you the start and end of the range for each pair. This can be done with regexp(data,'-','split') or a similar call

  3. Then you can loop over each pair, and generate the list of IDs in that range with startID:endID - but be careful here - the IDs can be very large. So you may need to use uint64 or similar to avoid overflow.

It turns out that doing this in Matlab is a bit of a pain, but julia (https://julialang.org/), which is very similar to Matlab, makes this a lot easier because it has built-in support for big integers.

Tips

Second problem

The second part of the problem is a bit more tricky. Now you have to find all IDs that are invalid in a more complicated way - any ID that’s made up of 2 or more repeating chunks e.g. 1212 (2 chunks of 12), 123123123 (3 chunks of 123), 999999 (6 chunks of 9), 12341234 (2 chunks of 1234) are all invalid.

# say
iChunk = 3
nChunks = Int(nDigits / iChunk)
# this will get you a list of all the chunks....
chunks = [SubString(str,1 + (i-1)*iChunk, i*iChunk) for i = 1:nChunks]
Day 2 animated gif

Code

Julia solution (not easy!)

Julia code / solution for the first part of that problem.

Second part solution of that problem.