Advent of Code: 2022 Day 4 in R

r
advent of code
puzzle
Published

September 9, 2023

Day 4: Camp Cleanup

See the puzzle instructions here.

Part 1

In how many assignment pairs does one range fully contain the other?

I will warn you that this post will show exactly how I’m exploring the problem and all the intermediate steps – it’s not a polished report on the end product. The first challenge here is reading in the data in a way that makes it useful to us. We can first get the lines:

test_data <- readLines("test_input.txt")
test_data
[1] "2-4,6-8" "2-3,4-5" "5-7,7-9" "2-8,3-7" "6-6,4-6" "2-6,4-8"

Next, we need to split out each line into the two Elves.

elf_list <- strsplit(test_data, ",")
elf_list
[[1]]
[1] "2-4" "6-8"

[[2]]
[1] "2-3" "4-5"

[[3]]
[1] "5-7" "7-9"

[[4]]
[1] "2-8" "3-7"

[[5]]
[1] "6-6" "4-6"

[[6]]
[1] "2-6" "4-8"

We can further get each elf’s range by splitting up the strings again. Here we have a list where each item in the list is a list of two 2-item vectors, each representing the start and end of each area for each elf.

elf_list_ranges <- lapply(elf_list, strsplit, split = "-")
elf_list_ranges
[[1]]
[[1]][[1]]
[1] "2" "4"

[[1]][[2]]
[1] "6" "8"


[[2]]
[[2]][[1]]
[1] "2" "3"

[[2]][[2]]
[1] "4" "5"


[[3]]
[[3]][[1]]
[1] "5" "7"

[[3]][[2]]
[1] "7" "9"


[[4]]
[[4]][[1]]
[1] "2" "8"

[[4]][[2]]
[1] "3" "7"


[[5]]
[[5]][[1]]
[1] "6" "6"

[[5]][[2]]
[1] "4" "6"


[[6]]
[[6]][[1]]
[1] "2" "6"

[[6]][[2]]
[1] "4" "8"

At this point I might switch over to manually iterating. I’m going to pull out just one pair for now to play around with.

pair <- elf_list_ranges[[1]]
for (range in pair) {
  print(readr::parse_number(range))
}
[1] 2 4
[1] 6 8

Now I need to convert that into sequences covering those points.

pair_ranges <- vector("list", 2)
for (i in seq_along(pair)) {
  range_numeric <- readr::parse_number(pair[[i]])
  area_range <- seq(range_numeric[1], range_numeric[2])
  pair_ranges[[i]] <- area_range
}
pair_ranges
[[1]]
[1] 2 3 4

[[2]]
[1] 6 7 8

Ok, that looks promising. Now I just have to check whether either range completely includes the other.

all(pair_ranges[[1]] %in% pair_ranges[[2]]) || all(pair_ranges[[2]] %in% pair_ranges[[1]])
[1] FALSE

Not bad. Now I have to put that all together and iterate over the list of lists.

get_character_list <- function(input_data) {
  strsplit(input_data, ",") |> 
    lapply(strsplit, split = "-")
}
character_list <- get_character_list(test_data)
character_list
[[1]]
[[1]][[1]]
[1] "2" "4"

[[1]][[2]]
[1] "6" "8"


[[2]]
[[2]][[1]]
[1] "2" "3"

[[2]][[2]]
[1] "4" "5"


[[3]]
[[3]][[1]]
[1] "5" "7"

[[3]][[2]]
[1] "7" "9"


[[4]]
[[4]][[1]]
[1] "2" "8"

[[4]][[2]]
[1] "3" "7"


[[5]]
[[5]][[1]]
[1] "6" "6"

[[5]][[2]]
[1] "4" "6"


[[6]]
[[6]][[1]]
[1] "2" "6"

[[6]][[2]]
[1] "4" "8"
get_area_ranges <- function(character_list) {
1  all_pairs <- vector("list", length(character_list))
2  for (i in seq_along(character_list)) {
3    pair_ranges <- vector("list", 2)
4    for (j in 1:2) {
5      endpoints <- readr::parse_number(character_list[[i]][[j]])
6      area <- seq(from = endpoints[1], to = endpoints[2])
      pair_ranges[[j]] <- area
    }
7    all_pairs[[i]] <- pair_ranges
  }
  all_pairs
}
area_ranges <- get_area_ranges(character_list)
1
Initialize an empty list to hold the results
2
Iterate over the pairs of elves
3
Initialize a list to contain each pair’s ranges
4
I know there are only 2 vectors in each list, one for each elf, so I can hardcode 1:2
5
Turn the character strings into numeric values
6
Generate the sequence and populate the pair_ranges list
7
Populate the all_pairs list
area_ranges
[[1]]
[[1]][[1]]
[1] 2 3 4

[[1]][[2]]
[1] 6 7 8


[[2]]
[[2]][[1]]
[1] 2 3

[[2]][[2]]
[1] 4 5


[[3]]
[[3]][[1]]
[1] 5 6 7

[[3]][[2]]
[1] 7 8 9


[[4]]
[[4]][[1]]
[1] 2 3 4 5 6 7 8

[[4]][[2]]
[1] 3 4 5 6 7


[[5]]
[[5]][[1]]
[1] 6

[[5]][[2]]
[1] 4 5 6


[[6]]
[[6]][[1]]
[1] 2 3 4 5 6

[[6]][[2]]
[1] 4 5 6 7 8
check_overlap <- function(ranges) {
  all(ranges[[1]] %in% ranges[[2]]) || all(ranges[[2]] %in% ranges[[1]])
}
sapply(area_ranges, check_overlap)
[1] FALSE FALSE FALSE  TRUE  TRUE FALSE

Now to put it all together.

part_1 <- function(input_data) {
  input_data |> 
    get_character_list() |> 
    get_area_ranges() |> 
    sapply(check_overlap) |> 
    sum()
}
part_1(test_data)
[1] 2

That’s the correct answer for the example. Now to try it on the real thing:

real_data <- readLines("input.txt")
part_1(real_data)
[1] 494

That’s the right answer! One gold star!

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?

This should be easy. All I have to do is change check_overlap() to see if there is any() overlap instead of all().

check_any_overlap <- function(ranges) {
  any(ranges[[1]] %in% ranges[[2]]) || any(ranges[[2]] %in% ranges[[1]])
}

Put it together again:

part_2 <- function(input_data) {
  input_data |> 
    get_character_list() |> 
    get_area_ranges() |> 
    sapply(check_any_overlap) |> 
    sum()
}
part_2(test_data)
[1] 4

Real data:

part_2(real_data)
[1] 833

And that’s correct!

You can find all of my Advent of Code solutions on GitHub.