Geert's Advent of Code 2022 solutions in Python

Trying to solve the Advent of Code puzzles and simultanousnly learn some modern Python techniques and toolchains.


Project maintained by GeertLitjens Hosted on GitHub Pages — Theme by mattgraham

Day 4

Return to main page

Again not too complicated today, just some thinking about hwo to phrase the if statements. I implemented a lot of this type of code for assessing bounding box overlaps, so quite straightforward in 1D.

Part 1

However, as some of the Elves compare their section assignments with each other, they’ve noticed that many of the assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list of the section assignments for each pair (your puzzle input).

For example, consider the following list of section assignment pairs:

2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

For the first few pairs, this list means:

  • Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8).
  • The Elves in the second pair were each assigned two sections.
  • The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also got 7, plus 8 and 9.

This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this:

.234.....  2-4
.....678.  6-8

.23......  2-3
...45....  4-5

....567..  5-7
......789  7-9

.2345678.  2-8
..34567..  3-7

.....6...  6-6
...456...  4-6

.23456...  2-6
...45678.  4-8

Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are 2 such pairs.

The parsing was a bit more involved because I wanted to prepare it in ints for the subsequent functions. So first split the lines, then the pairs and then store the min and max.

def _parse_data(self: "DaySolution", input_data: str) -> AoCData:
    pairs = []
    for line in input_data.splitlines():
        pair1, pair2 = line.split(",")
        pair1_min, pair1_max = pair1.split("-")
        pair2_min, pair2_max = pair2.split("-")
        pairs.append(
            [[int(pair1_min), int(pair1_max)], [int(pair2_min), int(pair2_max)]]
        )
    return pairs

Two if-statements are needed to check if either pair 1 or 2 are fully within the other. Very straightforward.

def _solve_part1(self: "DaySolution", parsed_data: AoCData) -> AoCData:
    complete_overlap = 0
    for pair in parsed_data:
        if pair[0][0] >= pair[1][0] and pair[0][1] <= pair[1][1]:
            complete_overlap += 1
        elif pair[1][0] >= pair[0][0] and pair[1][1] <= pair[0][1]:
            complete_overlap += 1
    return complete_overlap

Part 2

It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all.

In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don’t overlap, while the remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:

  • 5-7,7-9 overlaps in a single section, 7.
  • 2-8,3-7 overlaps all of the sections 3 through 7.
  • 6-6,4-6 overlaps in a single section, 6.
  • 2-6,4-8 overlaps in sections 4, 5, and 6.

So, in this example, the number of overlapping assignment pairs is 4.

In how many assignment pairs do the ranges overlap?

I could imagine that this one could have led to some overengineering, but you actually only have to check whether the minimum of one of the ranges is within the other for both pairs.

def _solve_part2(self: "DaySolution", parsed_data: AoCData) -> AoCData:
    partial_overlap = 0
    for pair in parsed_data:
        if pair[1][1] >= pair[0][0] >= pair[1][0]:
            partial_overlap += 1
        elif pair[0][1] >= pair[1][0] >= pair[0][0]:
            partial_overlap += 1
    return partial_overlap

Return to main page