From 5c09e523e68f696df88c4f02afeb29e704ad45f2 Mon Sep 17 00:00:00 2001 From: kageru Date: Thu, 15 Dec 2022 14:09:41 +0100 Subject: [PATCH] optimize parsing --- 2022/src/bin/day15.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/2022/src/bin/day15.rs b/2022/src/bin/day15.rs index ac76b00..b08a332 100644 --- a/2022/src/bin/day15.rs +++ b/2022/src/bin/day15.rs @@ -1,4 +1,4 @@ -#![feature(test, result_option_inspect)] +#![feature(test, iter_array_chunks)] extern crate test; use aoc2022::{boilerplate, common::*}; @@ -8,14 +8,13 @@ const DAY: usize = 15; type Parsed = Vec<((i64, i64), u64)>; fn parse_input(raw: &str) -> Parsed { - raw.lines() - .map(|line| { - <[&str; 4]>::try_from(line[12..].replace(", y=", ",").replace(": closest beacon is at x=", ",").split(',').collect_vec()) - .unwrap() - .map(|s| s.parse().unwrap()) - }) - .map(|[x1, y1, x2, y2]| ((x1, y1), (x2, y2))) - .map(|(t1, t2)| (t1, manhattan(t1, t2))) + raw.replace(", y=", ",") + .replace(": closest beacon is at x=", ",") + .lines() + .flat_map(|line| line[12..].split(',')) + .map(|s| s.parse().unwrap()) + .array_chunks() + .map(|[x1, y1, x2, y2]| ((x1, y1), manhattan((x1, y1), (x2, y2)))) .collect() } @@ -38,7 +37,7 @@ fn part1(parsed: &Parsed, y: i64) -> i64 { match parsed.iter().filter(|&&((x1, y1), d)| x1 > x && y1.abs_diff(y) < d).min_by_key(|&&(p, d)| manhattan(p, (x, y)) - d) { Some(&((px, py), d)) => x = px - (d as i64 - (py.abs_diff(y) as i64)), None => break, - }; + } } }; }