Advent of Code 2022, Day 03 in Rust
December 03, 2022
Getting to the third day, things are a slight bit more complicated this time.
Solving the first part
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
andA
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:
1 2 3 4 5 6 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
throughz
have priorities 1 through 26.- Uppercase item types
A
throughZ
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
), and 19 (s
); the sum of these is157
.Find the item type that appears in both compartments of each rucksack. What is the sum of the priorities of those item types?
The first part isn't too much outside of some parsing each line of the text content. Let's get to work, shall we?
The above example gave us a result of 157
, so let's start with modifying the test result to 157.
1
2
3
4
5
#[test]
fn test_part_one() {
let input = advent_of_code::read_file("examples", 3);
assert_eq!(part_one(&input), Some(157));
}
Since we are finding the single letter that is shared between two strings, my idea is to convert them into HashSet
s, then finding the intersection between those two sets (basic set theory is powerful here!). Afterwards, I'll simply be converting the found character to its priority, then summing them up. I think the code below is rather self explanatory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
let mut answer: u32 = 0;
for line in input.lines() {
let (first, second) = line.split_at(line.len() / 2);
let mut a = HashSet::new();
let mut b = HashSet::new();
for c in first.chars() {
a.insert(c);
}
for c in second.chars() {
b.insert(c);
}
let intersect = a.intersection(&b).next().unwrap();
match intersect {
'a'..='z' => answer += *intersect as u32 - 96,
_ => answer += *intersect as u32 - 64 + 26,
}
}
Some(answer)
I splitted the input line into half (at the position length / 2
), then inserting each character of those two halves into two different HashSet
s. Then, I used the .intersection()
method onto the two, and processed that.
Why - 96
and - 64 + 26
? The characters a
to z
has the ASCII values of 97
to 122
, so to convert them back into the priority numbers the question wanted, I subtracted them by 96 to get the priority. Similarly, the ASCII values of A
to Z
are 65
to 90
, so I'll subtract them by 64, and then adding 26 to make them match the priorities given by the question.
Solving the second part
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:
1 2 3 vJrwpWtwJgWrhcsFMMfFFhFp jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL PmmdzqPrVvPwwTWBwgAnd the second group's rucksacks are the next three lines:
1 2 3 wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn ttgJtRGJQctTZtZT CrZsJsPPZsGzwwsLwLmpwMDwIn 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 is70
.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?
Well, this isn't much more than the first question, but to consider every 3 lines as a group, instead of two halves of one line.
Let's edit our test case to 70 first:
1
2
3
4
5
#[test]
fn test_part_two() {
let input = advent_of_code::read_file("examples", 3);
assert_eq!(part_two(&input), Some(70));
}
Because this part of the question isn't much more than parsing every 3 lines, I opted for a bunch of Rust iterator magic. I think explaining each line as comments would be easier, so below would be my code, with comments!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
pub fn part_two(input: &str) -> Option<u32> {
let answer = input
// grab the lines of the input as an iterator
.lines()
// iterate every 3 lines
.step_by(3)
// "zip" the iterator with another iterator
.zip(
input
.lines()
// skip through the first line (so we are taking the second line
// here)
.skip(1)
// iterate another 3 lines
.step_by(3)
// "zip" with another iterator
// I get the third line from the input text in the similar way
.zip(input.lines().skip(2).step_by(3)),
)
// map and flattens down the iterators
.flat_map(|(first, (second, third))| {
first
// get the characters of the first line
.chars()
// then finding the character that is contained in both the
// second and the third line
.find(|c| second.contains(*c) && third.contains(*c))
})
// collect the character into a String
.collect::<String>()
// grab its characters again
.chars()
// folding it with an accumulator - that is our answer
.fold(0, |acc, c| {
if c.is_ascii_lowercase() {
acc + (c as u32) - 96
} else {
acc + (c as u32) - 38
}
});
Some(answer)
}
Afterword
Once again, a parsing problem that takes more effort to think about the parsing method than actually solving the problem! Not too hard, just really annoying to think about how to code in an idiomatic way.
My code can be found on my GitHub.