Clean up parsing

This commit is contained in:
Adrian Groh 2024-12-14 12:36:03 +01:00
parent 830c450973
commit 8adfd753cb
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771

View File

@ -7,22 +7,26 @@ struct Robot {
velocity: IVec2, velocity: IVec2,
} }
#[rustfmt::skip]
fn parse(input: &str) -> Vec<Robot> { fn parse(input: &str) -> Vec<Robot> {
let re = Regex::new(r"-?\d+").unwrap(); let re = Regex::new(r"-?\d+").unwrap();
input.lines().map(|l| { input
let mut captures = re.captures_iter(l); .lines()
Robot { .map(|l| {
position: IVec2 { let &[px, py, vx, vy] = re
x: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), .captures_iter(l)
y: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), .take(4)
}, .map(|c| c.get(0).unwrap().as_str().parse().unwrap())
velocity: IVec2 { .collect::<Vec<_>>()
x: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), .as_slice()
y: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), 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 { fn get_pos_after_steps(robot: &Robot, steps: &i32, dimensions: &IVec2) -> IVec2 {