<- function(file) {
read_input readLines(file)
}
Advent of Code: 2022 Day 3 in R
Day 3: Rucksack Reorganization
One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that Elf didn’t quite follow the packing instructions, and so a few items now need to be rearranged.
Each rucksack has two large compartments. All items of a given type are meant to go into exactly one of the two compartments. The Elf that did the packing failed to follow this rule for exactly one item type per rucksack.
The Elves have made a list of all of the items currently in each rucksack (your puzzle input), but they need your help finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, a and A refer to different types of items).
The list of items for each rucksack is given as characters all on a single line. A given rucksack always has the same number of items in each of its two compartments, so the first half of the characters represent items in the first compartment, while the second half of the characters represent items in the second compartment.
For example, suppose you have the following list of contents from six rucksacks:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
- The first rucksack contains the items
vJrwpWtwJgWrhcsFMMfFFhFp
, which means its first compartment contains the itemsvJrwpWtwJgWr
, while the second compartment contains the itemshcsFMMfFFhFp.
The only item type that appears in both compartments is lowercasep
.- The second rucksack’s compartments contain
jqHRNqRjqzjGDLGL
andrsFMfFZSrLrFZsSL.
The only item type that appears in both compartments is uppercaseL
.- The third rucksack’s compartments contain
PmmdzqPrV
andvPwwTWBwg
; the only common item type is uppercaseP
.- The fourth rucksack’s compartments only share item type
v
.- The fifth rucksack’s compartments only share item type
t
.- The sixth rucksack’s compartments only share item type
s
.To help prioritize item rearrangement, every item type can be converted to a priority: - Lowercase item types a through z have priorities 1 through 26. - Uppercase item types A through Z have priorities 27 through 52.
In the above example, the priority of the item type that appears in both compartments of each rucksack is
16
(p
),38
(L
),42
(P
),22
(v
),20
(t
), and19
(s
); the sum of these is157.
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.
<- read_input("test_input.txt")
test_data <- strsplit(test_data, "") rucksack_list
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_list[[1]] rucksack
Now the two halves:
<- rucksack[1:(length(rucksack) / 2)]
half_1 <- rucksack[(length(rucksack) / 2 + 1):length(rucksack)] half_2
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
.
<- character(length(rucksack_list))
shared_items for (i in seq_along(rucksack_list)) {
<- rucksack_list[[i]][1:(length(rucksack_list[[i]]) / 2)]
half_1 <- rucksack_list[[i]][
half_2 length(rucksack_list[[i]]) / 2 + 1):length(rucksack_list[[i]])
(
]<- intersect(half_1, half_2)
shared_items[i] }
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.
<- function(letter) {
get_priority <- c(letters, LETTERS)
priorities 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.
<- function(sack) {
get_shared_item <- sack[1:(length(sack) / 2)]
half_1 <- sack[(length(sack) / 2 + 1):length(sack)]
half_2 intersect(half_1, half_2)
}
<- function(input_data) {
part_1 <- strsplit(input_data, "")
rucksack_list <- sapply(rucksack_list, get_shared_item)
shared_items <- sapply(shared_items, get_priority)
priorities sum(priorities)
}
This could also be written with the native R pipe:
<- function(input_data) {
part_1_pipe |>
input_data strsplit("") |>
sapply(get_shared_item) |>
sapply(get_priority) |>
sum()
}
Now to try it out:
<- read_input("input.txt")
real_data 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
As you finish identifying the misplaced items, the Elves come to you with another issue.
For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For efficiency, within each group of three Elves, the badge is the only item type carried by all three Elves. That is, if a group’s badge is item type B, then all three Elves will have item type B somewhere in their rucksack, and at most two of the Elves will be carrying any other item type.
The problem is that someone forgot to put this year’s updated authenticity sticker on the badges. All of the badges need to be pulled out of the rucksacks so the new authenticity stickers can be attached.
Additionally, nobody wrote down which item type corresponds to each group’s badges. The only way to tell which item type is the right one is by finding the one item type that is common between all three Elves in each group.
Every set of three lines in your list corresponds to a single group, but each group can have a different badge item type. So, in the above example, the first group’s rucksacks are the first three lines:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
And the second group’s rucksacks are the next three lines:
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
In the first group, the only item type that appears in all three rucksacks is lowercase
r
; this must be their badges. In the second group, their badge item type must beZ
.Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18 (
r
) for the first group and 52 (Z
) for the second group. The sum of these is 70.Find the item type that corresponds to the badges of each three-Elf group. What is the sum of the priorities of those item types?
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.
<- function(input_data) {
get_teams 1<- vector("list", length = length(input_data) / 3)
elf_teams 2<- 1
i while (i <= length(input_data)) {
3ceiling(i / 3)]] <- input_data[i:(i+2)]
elf_teams[[4<- i + 3
i
}
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
while
loop - 3
-
I use
[ceiling(i / 3)]
to find the next appropriate element ofelf_teams
to populate. Fori
in1:3
, the value is1
, fori
in4:6
, the value is2
, etc. Doing this means I don’t have to nest loops to iterate over bothelf_teams
andinput_data
. - 4
-
Increase
i
by 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.
<- function(team) {
get_badge <- strsplit(team, "")
elves intersect(
intersect(elves[[1]], elves[[2]]),
3]]
elves[[
) }
Now I put it together with get_priority()
to solve the whole thing.
<- function(input_data) {
part_2 <- get_teams(input_data)
teams <- sapply(teams, get_badge)
badges <- sapply(badges, get_priority)
priorities sum(priorities)
}
Again rewriting with the pipe, just for illustration:
<- function(input_data) {
part_2_pipe |>
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.