Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs.
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).
Set-Up
Today this was the most inefficient part of my solution. Originally read in the data as a table and then split out using gsub. This can be simplified by reading in as strings, replacing with commas in the strings, and then use the text argument in read.table.
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.
---title: "2022 - Day 4"format: html: code-fold: show code-tools: true code-block-bg: true code-block-border-left: "#AB63BD"---> Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs.>> 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).## Set-UpToday this was the most inefficient part of my solution. Originally read in the data as a table and then split out using `gsub`. This can be simplified by reading in as strings, replacing with commas in the strings, and then use the `text` argument in `read.table`.```{r setup}# Originalassignments <- read.csv("input.txt", header = FALSE, col.names = c("A", "B"))assignments$A1 <- with(assignments, as.numeric(sub("-.*", "", A)))assignments$A2 <- with(assignments, as.numeric(sub(".*-", "", A)))assignments$B1 <- with(assignments, as.numeric(sub("-.*", "", B)))assignments$B2 <- with(assignments, as.numeric(sub(".*-", "", B)))# Simplifiedassignments_clean <- read.table( text = gsub("-", ",", readLines("input.txt")), sep = ",", col.names = c("A1", "A2", "B1", "B2"))all.equal(assignments[, c("A1", "A2", "B1", "B2")], assignments_clean)```## Part 1> 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.```{r part_1}with(assignments, sum((A1 >= B1 & A2 <= B2) | (B1 >= A1 & B2 <= A2)))```## 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 how many assignment pairs do the ranges overlap?```{r part_2}with(assignments, sum((A1 <= B2 & A2 >= B2) | (B1 <= A2 & B2 >= A2)))```