optimize parsing

This commit is contained in:
kageru 2022-12-15 14:09:41 +01:00
parent f2a9208212
commit 5c09e523e6

View File

@ -1,4 +1,4 @@
#![feature(test, result_option_inspect)] #![feature(test, iter_array_chunks)]
extern crate test; extern crate test;
use aoc2022::{boilerplate, common::*}; use aoc2022::{boilerplate, common::*};
@ -8,14 +8,13 @@ const DAY: usize = 15;
type Parsed = Vec<((i64, i64), u64)>; type Parsed = Vec<((i64, i64), u64)>;
fn parse_input(raw: &str) -> Parsed { fn parse_input(raw: &str) -> Parsed {
raw.lines() raw.replace(", y=", ",")
.map(|line| { .replace(": closest beacon is at x=", ",")
<[&str; 4]>::try_from(line[12..].replace(", y=", ",").replace(": closest beacon is at x=", ",").split(',').collect_vec()) .lines()
.unwrap() .flat_map(|line| line[12..].split(','))
.map(|s| s.parse().unwrap()) .map(|s| s.parse().unwrap())
}) .array_chunks()
.map(|[x1, y1, x2, y2]| ((x1, y1), (x2, y2))) .map(|[x1, y1, x2, y2]| ((x1, y1), manhattan((x1, y1), (x2, y2))))
.map(|(t1, t2)| (t1, manhattan(t1, t2)))
.collect() .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) { 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)), Some(&((px, py), d)) => x = px - (d as i64 - (py.abs_diff(y) as i64)),
None => break, None => break,
}; }
} }
}; };
} }