Data
Each line contains a pair of sections which two elves should clean.
(sections_list <- read_table(
here("DATA", "2022", "input_day_04.txt"),
col_names = "section_pairs"
))
── Column specification ────────────────────────────────────────────────────────
cols(
section_pairs = col_character()
)
# A tibble: 1,000 × 1
section_pairs
<chr>
1 22-65,22-66
2 91-94,63-91
3 6-88,5-31
4 85-86,64-86
5 40-65,40-65
6 25-82,24-94
7 68-69,68-92
8 60-91,89-92
9 3-72,3-73
10 44-85,84-85
# ℹ 990 more rows
Cleaning the data a bit.
(
sections_list <- sections_list %>%
#extract sections for elves
separate(col = section_pairs, into = c("elf1", "elf2"), sep = ",") %>%
# extract minimums and maximums for each elf
separate(col = elf1, into = c("elf1_min", "elf1_max"), sep = "-") %>%
separate(col = elf2, into = c("elf2_min", "elf2_max"), sep = "-") %>%
mutate(across(.cols = everything(), .fns = as.numeric))
)
# A tibble: 1,000 × 4
elf1_min elf1_max elf2_min elf2_max
<dbl> <dbl> <dbl> <dbl>
1 22 65 22 66
2 91 94 63 91
3 6 88 5 31
4 85 86 64 86
5 40 65 40 65
6 25 82 24 94
7 68 69 68 92
8 60 91 89 92
9 3 72 3 73
10 44 85 84 85
# ℹ 990 more rows
Overlapping
Find entirely overlapping sections.
find_overlap <- function(min1, min2, max1, max2){
# check whether the second set of numbers is contained in the first
return(
(min2 >= min1) & (max2 <= max1)
)
}
(sections_list_entire_overlap <- sections_list %>%
rowwise() %>%
mutate(elf2_overlap = find_overlap(elf1_min, elf2_min, elf1_max, elf2_max)) %>%
mutate(elf1_overlap = find_overlap(elf2_min, elf1_min, elf2_max, elf1_max)))
# A tibble: 1,000 × 6
# Rowwise:
elf1_min elf1_max elf2_min elf2_max elf2_overlap elf1_overlap
<dbl> <dbl> <dbl> <dbl> <lgl> <lgl>
1 22 65 22 66 FALSE TRUE
2 91 94 63 91 FALSE FALSE
3 6 88 5 31 FALSE FALSE
4 85 86 64 86 FALSE TRUE
5 40 65 40 65 TRUE TRUE
6 25 82 24 94 FALSE TRUE
7 68 69 68 92 FALSE TRUE
8 60 91 89 92 FALSE FALSE
9 3 72 3 73 FALSE TRUE
10 44 85 84 85 TRUE FALSE
# ℹ 990 more rows
Find the total.
tot_overlap <- sections_list_entire_overlap %>%
ungroup() %>%
mutate(any_overlap = elf2_overlap | elf1_overlap) %>%
summarise(sum_overlap = sum(any_overlap)) %>%
pull()
The total number of entirely overlapping regions is 584! 🥳
Find overlapping at all
find_any_overlap <- function(min1, min2, max1, max2){
# check whether the second set of numbers is contained in the first
return(
((min2 >= min1) & (min2 <= max1)) |
((max2 <= max1) & (max2 >= min1))
)
}
(sections_list_any_overlap <- sections_list %>%
rowwise() %>%
mutate(elf2_overlap = find_any_overlap(elf1_min, elf2_min, elf1_max, elf2_max)) %>%
mutate(elf1_overlap = find_any_overlap(elf2_min, elf1_min, elf2_max, elf1_max)))
# A tibble: 1,000 × 6
# Rowwise:
elf1_min elf1_max elf2_min elf2_max elf2_overlap elf1_overlap
<dbl> <dbl> <dbl> <dbl> <lgl> <lgl>
1 22 65 22 66 TRUE TRUE
2 91 94 63 91 TRUE TRUE
3 6 88 5 31 TRUE TRUE
4 85 86 64 86 TRUE TRUE
5 40 65 40 65 TRUE TRUE
6 25 82 24 94 FALSE TRUE
7 68 69 68 92 TRUE TRUE
8 60 91 89 92 TRUE TRUE
9 3 72 3 73 TRUE TRUE
10 44 85 84 85 TRUE TRUE
# ℹ 990 more rows
Find total
tot_any_overlap <- sections_list_any_overlap %>%
ungroup() %>%
mutate(any_overlap = elf2_overlap | elf1_overlap) %>%
summarise(sum_overlap = sum(any_overlap)) %>%
pull()
The total number of at tall overlapping regions is 933! 🥳