Function aoc::y15d08::y15d08p2

source ·
pub fn y15d08p2(input: &str) -> u32
Expand description

The solution for part two of the day eight challenge.

Even easier than part one, we loop through all of the input lines calculating the “code” sum the same as in part one (with the .len() function). To calculate the “encoded” sum we start with 2 to account for the new " that are added to the start and end of the string. Then we loop through the characters and if we encounter the " or \ characters we increment by two to account for the escape otherwise we increment by one as its standard. Add the tallies for the line to the totals and then return the “encoded” sum minus the “code” sum as requested by the prompt.

§Example

// probably read this from the input file...
let input = "\"a\"b\"\n\"\"\n\"\x23\"";
assert_eq!(y15d08p2(input), 13);