Module aoc::y22d03

source ·
Expand description

Advent of Code 2022 Day 3: https://adventofcode.com/2022/day/3

The day three challenge basically amounts to finding characters in strings.

For the first part we need to find a single character that appears in both the first half and the second half of each string. I approached this by looping through each line and in the first half of the string adding each character to a std::collections::HashSet as it’s seen. Then in the second half of the string we check each character as we pass to see if it exists in the Set – once we find a match we’re done for that line.

I didn’t try, but it may be faster or more efficient to just split the string in two and then loop through the second half only and just run a contains check on the entire first half string.

This is similar to the approach that I took for the second part of the challenge. We loop through each group, but we actually only loop through the characters in the third string of the group and check if both of the first two strings contain the character in which case we’re done.

Finally, for determining the priority we just define a string with the priorities in order so that we can do a simple lookup for a given character (plus one because the string is zero-indexed).

Functions§

  • The solution for the day three challenge. In reality calls a second function based on the second argument (part 1 or part 2).