pub fn y15d08p1(input: &str) -> u32
Expand description
The solution for part one of the day eight challenge.
Given the input string we initialize the total sums of each type and then
loop over each string. The “code” representation we can find by simply
calling the .len()
function (and converting it to a u32
). To calculate
the “memory” representation we loop over all of the characters. If we have
previously encountered the escape character then we check if we have an x
which means a hexadecimal escape (and we need to skip the next two
characters) or not in which case we just move on without incrementing the
counter. If we have a skip value set then decrement it and move on without
incrementing the counter. If we encounter the "
we don’t increment the
counter because it’s the start/end. Otherwise we increment the counter.
If we’ve encountered the escape character then trigger the behavior
described above for the next loop iteration. Finally, add the “memory” sum
to the running total and then return the “code” sum minus the “memory” sum
as requested by the prompt.
§Example
// probably read this from the input file...
let input = "\"a\"b\"\n\"\"\n\"\x23\"";
assert_eq!(y15d08p1(input), 7);