Part 1
In this puzzle, we are given a list of lines containing number ranges. In part 1, we should find the total number of elements contained in all the ranges. Elements contained in multiple ranges are counted multiple times.Part 2
In part 2, we should look at the ranges in pairs of 2. We should still count the number of elements in those ranges, but elements contained multiple times in this single pair should only be counted once.
Since the ranges are continous, we can calculate the number of elements in two ranges combined by adding the size of the two ranges and substracting the overlapping elements. The number of overlapping elements is `overlap = max(0, range_size(max(min_1, min_2), min(max_1, max_2)))`. Iterating the ranges in pairs and summing the combined number of elements gives us the answer.
Part 3
In part 3, we should be looking at pairs of pairs and find the 2 neighbouring pairs containing the most distinct elements. Again, elements contained in multiples of theses 4 ranges should only be counted once.
For this part, we wrote a function for combining multiple ranges. First we sort all ranges by their start. Starting from the first range, whenever we encounter another range we either extend the current range or start a new range. Using this function we can now take the ranges in neighbouring pairs, calculate the combined size and update the largest combined size found until now for the answer.
No comments:
Post a Comment