2025-advent-of-code

following along with the advent of code 2025 (Matlab)

View on GitHub

Day 05

Another problem involving large ranges of IDs. This time the IDs can be very large - up to X digits long! So you have to be careful about how you represent them in your code. My solution here is to just head straight to julia which has built-in support for big integers.

Tips for first problem

  1. The input file contains a list of ranges of IDs, e.g. 11-30,12344-14999,... - but then it’s followed by actual IDs that you have to check against those ranges. So parsing in two parts … or my read: read into a vector of strings, then find the first empty line which signals the break between the two parts.

  2. You have to parse the file. 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

  3. 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

  4. Then you can loop over each pair, and generate the list of IDs in that range with startID:endID - but be careful here - BigInteger support is needed for very large numbers.

Second problem

Same idea - working with sets for each ID is going to be too big. So you need to merge the ranges first, then count the total number of IDs covered by the merged ranges. This means working with the start and end points of each range.

# print the list / this will print A-B for each range
[print("$(a)-$(b)\n") for (a,b) in validIdRanges]

# endIndex - startIndex + 1 gives the range
# this will calculate the difference for each row
# and store in a vector
result = [(b-a)+1 for (a,b) in validIdRanges] 

result |> println # check out infix operator!!
Day 5 animated gif

Code

Julia solution

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

Second part solution of that problem.