From a4ad70ddd3ba5c57a507e5c3c3bcdf8a11a878b3 Mon Sep 17 00:00:00 2001 From: kageru Date: Wed, 14 Dec 2022 23:46:52 +0100 Subject: [PATCH] optimize parsing --- 2022/src/bin/day14.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/2022/src/bin/day14.rs b/2022/src/bin/day14.rs index c2b56ee..d188856 100644 --- a/2022/src/bin/day14.rs +++ b/2022/src/bin/day14.rs @@ -14,15 +14,16 @@ type Parsed = (Cave, usize); fn parse_input(raw: &str) -> Parsed { let mut cave = [[false; CAVE_HEIGHT]; CAVE_WIDTH]; let mut max_y = 0; + let mut segments: Vec<(usize, usize)> = Vec::with_capacity(10); for line in raw.lines() { - let segments: Vec<(usize, usize)> = - line.split(" -> ").map(|s| s.split_once(',').unwrap()).map(|(x, y)| (parse_num::(x) - X_OFFSET, parse_num(y))).collect(); + segments + .extend(line.split(" -> ").map(|s| s.split_once(',').unwrap()).map(|(x, y)| (parse_num::(x) - X_OFFSET, parse_num(y)))); for &[(x1, y1), (x2, y2)] in segments.array_windows() { - max_y = max_y.max(y1).max(y2); for (x, y) in ((x1.min(x2))..=(x1.max(x2))).flat_map(|x| repeat(x).zip((y1.min(y2))..=(y1.max(y2)))) { cave[x][y] = true; } } + max_y = max_y.max(segments.drain(..).map(|(_, y)| y).max().unwrap()); } (cave, max_y) }