Function aoc::y15d03::y15d03

source ·
pub fn y15d03(input: &str, santas: u32) -> u32
Expand description

The solution for the day three challenge.

The function takes the input as a string and the number of Santa’s that are delivering presents as the second argument assuming that the instructions are consumed n-at-a-time where n is the number of Santa’s (as described in the challenge for 2 Santa’s). The difference between part one and two of the challenge is therefore the number of Santa’s participating: 1 or 2.

The solution is to split the input into number-of-santas-sized chunks and then loop over each chunk. Then, for each chunk we loop through each Santa and check the instruction that they have using their index as an index in the given chunk. We adjust their position based on the character (instruction that we found) and then increment (or initialize to 1 present) the house corresponding to their position. Finally we return the length of the houses collection as the response to the challenge.

A more naïve approach could have been to make the Santa loop the outer loop and the chunk loop the inner loop i.e., use a single position tracker (similar to the original solution to part one of the challenge) and then reset it for each Santa and then use each Santa “i” as the index to pull out of each chunk as we loop through the chunks collection – but this results in looping through the chunks loop santa-times instead of just once through, though it hardly makes a difference for the small inputs and number of Santa’s that we actually have.

§Example

let input = "^<>^vv<>"; // probably read this from the input file...
assert_eq!(y15d03(input, 1), 5);
assert_eq!(y15d03(input, 2), 6);