From: Greg Burri Date: Tue, 10 Dec 2024 19:18:03 +0000 (+0100) Subject: Down to 65 μs from 80 μs (average on 1000 runs) X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=d8c9ec5230e1a2fdcfbfa383e9dc52b7528314bc;p=advent_of_code_2024.git Down to 65 μs from 80 μs (average on 1000 runs) --- diff --git a/src/day10.rs b/src/day10.rs index 43dd221..ca7c7f6 100644 --- a/src/day10.rs +++ b/src/day10.rs @@ -2,30 +2,30 @@ use std::io::BufRead; use nalgebra::DMatrix; -type Map = DMatrix; +type Map = DMatrix; type Position = (usize, usize); pub fn read_map(reader: &mut dyn BufRead) -> (Map, Vec) { - let mut map = Map::default(); let mut start_positions: Vec = Vec::new(); + let mut data: Vec = 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 {