pub fn y15d06p2(input: &str) -> u64
Expand description
The solution part two of the day six challenge.
Part two is both similar and dissimilar to part one. At first I wanted to
manage the brightness count in a two-dimensional array of u32
s but I
kept overflowing the stack so instead we maintain a
std::collections::HashMap
of the brightness of each lights coordinates.
If the lights current brightness is zero (or would result in zero after the
operation) it does not have an entry in the map (or has its entry removed).
So, given our input as a string like in part one, we start by parsing each instruction and then loop through the light positions defined by the instruction. As mentioned in the prompt “turn on” means increase the brightness by one, “turn off” means decrease it by one (but not below zero), and “toggle” means increase the brightness by two. As we loop through each coordinate we create missing entries (lights that currently have a brightness of zero) and remove entries (lights whose brightness would become zero) as necessary. Finally, to compute the total brightness we just need to add the value of all of the lights that we’re currently tracking (i.e., have a brightness of at least one).
§Example
// probably read this from the input file...
let input = concat![
"turn on 102,12 through 400,40\n",
"turn off 100,10 through 203,23\n",
"turn on 15,14 through 16,15\n",
"toggle 20,21 through 22,21",
];
assert_eq!(y15d06p2(input), 7457);