Hello! We are on Day 06 of Advent of Code. Let’s explore how we can solve this puzzle.
Spoilers ahead! The full solution to the problem is available here.
Problem - Part 01:
Given a batch of inputs (groups) separated by an empty line, where every character in each line represents a question which is answered as “yes”. We need find the sum of counts of the unique questions answered by anyone in the group.
Sample Input:
This list represents answers from five groups:
In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 = 11.
Let’s write the function that counts the answers by anyone. The algorithm for this function would be as follows:
- Iterate through every line in the input batch. Initialize an empty_index that keeps track of the last line index which is empty.
- If the current line is empty, we get all the lines before the current index, and then join them together into a String. The joined String will have duplicate characters which we need to ignore, so we need to deduplicate the String.
- The length of the deduped string is the count for the answer.
- Once we have the counts for all the answers, we can sum them all to get the
sum_of_counts
.
In order to remove duplicate characters, we write a dedup_chars
function that uses a HashSet.
That’s all for Part 01. Let’s move on to Part 02.
**Problem - Part 02 **
Given a batch of inputs (groups) separated by an empty line, where every character in each line represents a question which is answered as “yes”. We need find the sum of counts of the questions answered commonly by everyone in the group.
Although the problem statement looks similar, the approach to solving this is slightly different.
Let’s write the function that counts the answers by everon. The algorithm for this function would be as follows:
- Iterate through every line in the input batch. Initialize an empty_index that keeps track of the last line index which is empty.
- If the current line is empty, we get all the lines before the current index and collect them into a vector representing the group. If the group only has one person, the number of unique characters in the answers, represent its count.
- If the group has more than person, we need to find the answer common within the group. Since the group is represented as a vec, we can find the intersection of the values in the vec.
- Once we have the counts for all the answers, we can sum them all to get the
sum_of_counts
.
The sum returned by the count_answers_by_everyone
is the answer for Part 02 of today’s puzzle.
That’s all for Day 06. See you tomorrow 👋