From 8adfd753cbb5532dcac1465500f82c6197d09f32 Mon Sep 17 00:00:00 2001 From: Adrian Groh Date: Sat, 14 Dec 2024 12:36:03 +0100 Subject: [PATCH] Clean up parsing --- day14/src/main.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/day14/src/main.rs b/day14/src/main.rs index 9fecde9..bbc0eb0 100644 --- a/day14/src/main.rs +++ b/day14/src/main.rs @@ -7,22 +7,26 @@ struct Robot { velocity: IVec2, } -#[rustfmt::skip] fn parse(input: &str) -> Vec { let re = Regex::new(r"-?\d+").unwrap(); - input.lines().map(|l| { - let mut captures = re.captures_iter(l); - Robot { - position: IVec2 { - x: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), - y: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), - }, - velocity: IVec2 { - x: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), - y: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), + input + .lines() + .map(|l| { + let &[px, py, vx, vy] = re + .captures_iter(l) + .take(4) + .map(|c| c.get(0).unwrap().as_str().parse().unwrap()) + .collect::>() + .as_slice() + else { + panic!() + }; + Robot { + position: IVec2 { x: px, y: py }, + velocity: IVec2 { x: vx, y: vy }, } - } - }).collect() + }) + .collect() } fn get_pos_after_steps(robot: &Robot, steps: &i32, dimensions: &IVec2) -> IVec2 {