Data
Each line is a list of items in one rucksack.
(rucksack_data <- read_table (
here ("DATA" , "2022" , "input_day_03.txt" ),
col_names = FALSE
))
── Column specification ────────────────────────────────────────────────────────
cols(
X1 = col_character()
)
# A tibble: 300 × 1
X1
<chr>
1 DMwrszrfMzSSCpLpfCCn
2 RMvhZhQqlvhMvRtbvbcPclPlncddppLTdppd
3 tVMQhFtjjWmsFJsmsW
4 trRtvNhfJhSzzSTFVhQQZQhHGphP
5 CnLMBWLwDMgMcwwdngdHGPVTQGpTHZdGPGpd
6 LLDqcDgwqCMnLWqtvzrzbbtJqPjJ
7 wQQwHNQLmbWQbQRHwHNFBbwqPfjqlzRMGRqzpSfvPlzplM
8 nCtGCZZtsGsrtDMZpfMpSlMlvlZq
9 cJctJCgVJsCJnDTsCthGhGLwBWBbbQmLbgQLQQdWbbbQ
10 ZWnNlTNTnhhQQlDNdmmpwrrrqBwjwjZd
# ℹ 290 more rows
Find the repeating item
First, we need to find the middle of each string and divide.
rucksack_divided <- map (
rucksack_data$ X1,
function (item_list){
split_at <- str_length (item_list)/ 2
out_strings <- c (
str_sub (item_list, start = 1 , end = split_at),
str_sub (item_list, start = split_at + 1 , end = - 1 )
)
return (out_strings)
}
)
rucksack_divided[[1 ]]
[1] "DMwrszrfMz" "SSCpLpfCCn"
Next, compare the strings in each compartment to find the repeating item for each rucksack.
repeating_items <- map (
rucksack_divided,
function (compartments){
unique_contents <- map (
compartments,
function (item_list){
strsplit (item_list, split = "" ) %>%
unlist () %>%
unique ()
}
)
return (Reduce (f = intersect, x = unique_contents))
}
)
repeating_items[[1 ]]
Checking - do I get only one item per rucksack?
lengths (repeating_items) %>%
unique ()
✔️ Yes, great!
Priority of items
Lowercase letters have priorities from 1 through 26, uppercase letters - from 27 through 52.
priority_low <- str_locate (
paste0 (letters, collapse = "" ), letters
)[, "start" ]
priority_low <- tibble (
letter = letters,
prior = priority_low
)
priority_low
# A tibble: 26 × 2
letter prior
<chr> <int>
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6
7 g 7
8 h 8
9 i 9
10 j 10
# ℹ 16 more rows
priority_up <- str_locate (
paste0 (LETTERS, collapse = "" ), LETTERS
)[, "start" ] + 26
priority_up <- tibble (
letter = LETTERS,
prior = priority_up
)
priority_up
# A tibble: 26 × 2
letter prior
<chr> <dbl>
1 A 27
2 B 28
3 C 29
4 D 30
5 E 31
6 F 32
7 G 33
8 H 34
9 I 35
10 J 36
# ℹ 16 more rows
priority_all <- bind_rows (
priority_low, priority_up
)
Now, find the sum of the priorities of repeated items.
total_priority <- tibble (
repeat_item = unlist (repeating_items)
) %>%
left_join (
priority_all,
by = c ("repeat_item" = "letter" )
) %>%
summarise (sum_priority = sum (prior))
The total priority is 7811! 🥳
Group badges
Each three elves create a group and they are recognized by the item that they carry. This item is the only item that is shared by these elves.
# there are 3 members in each group
no_members <- 3
(rucksack_groups <- rucksack_data %>%
add_column (group_no =
rep (
seq (1 : (nrow (rucksack_data)/ no_members)),
each = no_members
)
))
# A tibble: 300 × 2
X1 group_no
<chr> <int>
1 DMwrszrfMzSSCpLpfCCn 1
2 RMvhZhQqlvhMvRtbvbcPclPlncddppLTdppd 1
3 tVMQhFtjjWmsFJsmsW 1
4 trRtvNhfJhSzzSTFVhQQZQhHGphP 2
5 CnLMBWLwDMgMcwwdngdHGPVTQGpTHZdGPGpd 2
6 LLDqcDgwqCMnLWqtvzrzbbtJqPjJ 2
7 wQQwHNQLmbWQbQRHwHNFBbwqPfjqlzRMGRqzpSfvPlzplM 3
8 nCtGCZZtsGsrtDMZpfMpSlMlvlZq 3
9 cJctJCgVJsCJnDTsCthGhGLwBWBbbQmLbgQLQQdWbbbQ 3
10 ZWnNlTNTnhhQQlDNdmmpwrrrqBwjwjZd 4
# ℹ 290 more rows
find_common_item <- function (items_list){
unique_items <- map (items_list, function (items){
str_split (items, pattern = "" ) %>%
unlist () %>%
unique ()
})
return (
Reduce (intersect, unique_items)
)
}
(rucksack_groups <- rucksack_groups %>%
group_by (group_no) %>%
summarise (
common_item = find_common_item (X1)
) %>%
left_join (priority_all, by = c ("common_item" = "letter" )))
# A tibble: 100 × 3
group_no common_item prior
<int> <chr> <dbl>
1 1 M 39
2 2 P 42
3 3 G 33
4 4 l 12
5 5 s 19
6 6 t 20
7 7 Z 52
8 8 n 14
9 9 p 16
10 10 z 26
# ℹ 90 more rows
sum_prior <- rucksack_groups %>%
summarise (sum_prior = sum (prior)) %>%
pull ()
The total priority is 2639! 🥳