Function aoc::year2022::day08::parse

source ยท
pub fn parse(input: &str) -> (usize, Vec<i8>)
Expand description

Convert a 2D grid of ASCII digits into a 1D vec of heights.

Each height is multipled by 6. For part 1 this makes no difference, but for part 2 this helps with the bit manipulation.

To convert from 2D co-ordinates to an index, the formula is y * width + x. For the sample grid of width 5, the top middle point at (2, 0) is at index 0 * 5 + 2 = 2 and the point directly below (2, 1) is at index 1 * 5 + 2 = 7.

Using a 1D vec instead of a vec of vecs is faster for 2 reasons:

  • Avoids an intermediate pointer lookup for each access.
  • Better cache locality as the memory locations are adjacent and not potentially scattered all over the heap.