use nalgebra::DMatrix;
-type Map = DMatrix<u32>;
+type Map = DMatrix<u8>;
type Position = (usize, usize);
pub fn read_map(reader: &mut dyn BufRead) -> (Map, Vec<Position>) {
- let mut map = Map::default();
let mut start_positions: Vec<Position> = Vec::new();
+ let mut data: Vec<u8> = Vec::new();
+ let mut nb_rows = 0;
+
for (i, l) in reader.lines().enumerate() {
- if map.nrows() < i + 1 {
- map = map.insert_row(i, 0);
- }
+ nb_rows += 1;
for (j, c) in l.unwrap().chars().enumerate() {
- if map.ncols() < j + 1 {
- map = map.insert_column(j, 0);
- }
- let level = c.to_digit(10).unwrap();
- map[(i, j)] = level;
+ let level = c.to_digit(10).unwrap() as u8;
+ data.push(level);
if level == 0 {
start_positions.push((i, j));
}
}
}
- (map, start_positions)
+ (
+ Map::from_row_slice(nb_rows, data.len() / nb_rows, &data),
+ start_positions,
+ )
}
pub fn score(map: &Map, start_positions: &[Position], multiple_paths: bool) -> u32 {