read_input <- function(file) {
readLines(file)
}Advent of Code: 2022 Day 3 in R
Day 3: Rucksack Reorganization
See the puzzle instructions here.
Part 1
Find the item type that appears in both compartments of each rucksack. What is the sum of the priorities of those item types?
This one is simple to read in:
Unfortunately for me, R is not the friendliest language for string manipulation, but I think this will be straightforward enough. Whereas a language like Python is able to treat a multi-character string as a list and iterate through it with ease, in R each string is an atomic vector that cannot be subdivided further. So we need to explicitly split the string before we operate on it.
I’ll first be working with the test data first.
test_data <- read_input("test_input.txt")
rucksack_list <- strsplit(test_data, "")We now have a list where each element is a vector of individual characters from each element in test_data:
rucksack_list[[1]]
[1] "v" "J" "r" "w" "p" "W" "t" "w" "J" "g" "W" "r" "h" "c" "s" "F" "M" "M" "f"
[20] "F" "F" "h" "F" "p"
[[2]]
[1] "j" "q" "H" "R" "N" "q" "R" "j" "q" "z" "j" "G" "D" "L" "G" "L" "r" "s" "F"
[20] "M" "f" "F" "Z" "S" "r" "L" "r" "F" "Z" "s" "S" "L"
[[3]]
[1] "P" "m" "m" "d" "z" "q" "P" "r" "V" "v" "P" "w" "w" "T" "W" "B" "w" "g"
[[4]]
[1] "w" "M" "q" "v" "L" "M" "Z" "H" "h" "H" "M" "v" "w" "L" "H" "j" "b" "v" "c"
[20] "j" "n" "n" "S" "B" "n" "v" "T" "Q" "F" "n"
[[5]]
[1] "t" "t" "g" "J" "t" "R" "G" "J" "Q" "c" "t" "T" "Z" "t" "Z" "T"
[[6]]
[1] "C" "r" "Z" "s" "J" "s" "P" "P" "Z" "s" "G" "z" "w" "w" "s" "L" "w" "L" "m"
[20] "p" "w" "M" "D" "w"
The first thing we need to do is divide each rucksack into its two halves. To start with, I’m going to pull out the first rucksack and work only with that so that I can get the puzzle logic down before applying it to the entire list.
rucksack <- rucksack_list[[1]]Now the two halves:
half_1 <- rucksack[1:(length(rucksack) / 2)]
half_2 <- rucksack[(length(rucksack) / 2 + 1):length(rucksack)]Next, I can get the intersect() of each element to find the overlap:
intersect(half_1, half_2)[1] "p"
Lowercase p is correct. Now I just need to code that process up to apply it to each element in rucksack_list.
shared_items <- character(length(rucksack_list))
for (i in seq_along(rucksack_list)) {
half_1 <- rucksack_list[[i]][1:(length(rucksack_list[[i]]) / 2)]
half_2 <- rucksack_list[[i]][
(length(rucksack_list[[i]]) / 2 + 1):length(rucksack_list[[i]])
]
shared_items[i] <- intersect(half_1, half_2)
}That’s the hardest part. Now just to calculate the priorities. I make a vector that combines the lower case letters and the uppercase letters, then I can get the priority value using which() to pull out the position of any given letter.
get_priority <- function(letter) {
priorities <- c(letters, LETTERS)
which(priorities == letter)
}
sum(sapply(shared_items, get_priority))[1] 157
Which is the correct answer for the example.
Now I’ll rewrite this as clean, clear functions to use for the real input. I already have get_priority() so I don’t need to rewrite that.
get_shared_item <- function(sack) {
half_1 <- sack[1:(length(sack) / 2)]
half_2 <- sack[(length(sack) / 2 + 1):length(sack)]
intersect(half_1, half_2)
}
part_1 <- function(input_data) {
rucksack_list <- strsplit(input_data, "")
shared_items <- sapply(rucksack_list, get_shared_item)
priorities <- sapply(shared_items, get_priority)
sum(priorities)
}This could also be written with the native R pipe:
part_1_pipe <- function(input_data) {
input_data |>
strsplit("") |>
sapply(get_shared_item) |>
sapply(get_priority) |>
sum()
}Now to try it out:
real_data <- read_input("input.txt")
part_1(real_data)[1] 7691
part_1_pipe(real_data)[1] 7691
That’s the right answer, and both versions of the function agree!
Part 2
The biggest challenge here is to group the elves in their teams of three. After that, it’s mostly a repeat of the previous example.
get_teams <- function(input_data) {
1 elf_teams <- vector("list", length = length(input_data) / 3)
2 i <- 1
while (i <= length(input_data)) {
3 elf_teams[[ceiling(i / 3)]] <- input_data[i:(i+2)]
4 i <- i + 3
}
elf_teams
}- 1
- Whenever possible, it’s best to construct a list or vector of the appropriate size and populate it, rather than appending onto the end of a list or vector. It’s much more memory efficient, and “vector growing” can actually crash your program if you do it enough times, like in a very long loop.
- 2
-
Initialize a counter variable and start a
whileloop - 3
-
I use
[ceiling(i / 3)]to find the next appropriate element ofelf_teamsto populate. Foriin1:3, the value is1, foriin4:6, the value is2, etc. Doing this means I don’t have to nest loops to iterate over bothelf_teamsandinput_data. - 4
-
Increase
iby 3 each time so we skip forward to the start of the next team
get_teams(test_data)[[1]]
[1] "vJrwpWtwJgWrhcsFMMfFFhFp" "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL"
[3] "PmmdzqPrVvPwwTWBwg"
[[2]]
[1] "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn" "ttgJtRGJQctTZtZT"
[3] "CrZsJsPPZsGzwwsLwLmpwMDw"
Next, I create a function to find the badge, which is just a small rewrite of get_shared_item(). I combine that function with strsplit() just to compress things a bit. I do have to nest intersect() because it only operates on two vectors.
get_badge <- function(team) {
elves <- strsplit(team, "")
intersect(
intersect(elves[[1]], elves[[2]]),
elves[[3]]
)
}Now I put it together with get_priority() to solve the whole thing.
part_2 <- function(input_data) {
teams <- get_teams(input_data)
badges <- sapply(teams, get_badge)
priorities <- sapply(badges, get_priority)
sum(priorities)
}Again rewriting with the pipe, just for illustration:
part_2_pipe <- function(input_data) {
input_data |>
get_teams() |>
sapply(get_badge) |>
sapply(get_priority) |>
sum()
}Let’s try it on the test data.
part_2(test_data)[1] 70
part_2_pipe(test_data)[1] 70
That’s right! Now to try on the real data:
part_2(real_data)[1] 2508
Beautiful! Another successful AoC puzzle!
You can find all of my Advent of Code solutions on GitHub.