From d8c9ec5230e1a2fdcfbfa383e9dc52b7528314bc Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Tue, 10 Dec 2024 20:18:03 +0100 Subject: [PATCH] =?utf8?q?Down=20to=2065=20=CE=BCs=20from=2080=20=CE=BCs?= =?utf8?q?=20(average=20on=201000=20runs)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/day10.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) 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 { -- 2.45.2