Adventures in Rust - Part 3
The previous two [1] [2] posts were about part1 of the Day3 problem.
Since part2 of the Day3 problem is not visible unless part1 is completed, will paste the problem statement here:
Problem
Approaches
Open the file 5 times and each time run the tree counting logic with given right and down increments and multiply the result obtained
Read the file once, per line check the index 5 times as per the right steps. In case of down 2 - skip alternate lines. Return a vector, each element representing per line a tuple of 5 elements - 1 meaning that there is a tree at expected position. Note in case of down 2 the value of lines skipped will be 0. Iterate over the vector and maintain sum per tuple index and finally multiply all these sums.
The problem with this approach however is that once we iterate the string to find the nth character, we cannot call nth again as the iterator has progressed further. Hence we need to clone the strings.
Code
The tricky part is the down2
--snip--
if (line_no % 2 == 0 )&&(line_no >1 ) {
tempeven = line_no/2;
if tempeven >= total_col {
tempeven = tempeven % total_col;
}
let idxeven : usize = (tempeven).try_into().unwrap();
let seven = s1.clone();
if seven.chars().nth(idxeven).unwrap() == '#'
{
valeven = 1;
}
else {
valeven = 0;
}
}
-- snip --Full code here.
Almost forgot the brag!! See you soon with Day4.
Reference
[3] Accessing tuples : https://doc.rust-lang.org/reference/expressions/tuple-expr.html


