Clean up parsing

This commit is contained in:
Adrian Groh 2024-12-14 12:33:10 +01:00
parent 6d4312ccd4
commit a68e7f0f03
Signed by: Gobidev
GPG Key ID: 3AA3153E98B0D771

View File

@ -10,20 +10,27 @@ struct Machine {
y3: isize, y3: isize,
} }
#[rustfmt::skip]
fn parse(input: &str) -> Vec<Machine> { fn parse(input: &str) -> Vec<Machine> {
let re = Regex::new(r"\d+").unwrap(); let re = Regex::new(r"\d+").unwrap();
input input
.split("\n\n") .split("\n\n")
.map(|m| { .map(|m| {
let mut captures = re.captures_iter(m); let &[x1, y1, x2, y2, x3, y3] = re
.captures_iter(m)
.take(6)
.map(|d| d.get(0).unwrap().as_str().parse().unwrap())
.collect::<Vec<_>>()
.as_slice()
else {
panic!()
};
Machine { Machine {
x1: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), x1,
y1: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), y1,
x2: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), x2,
y2: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), y2,
x3: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), x3,
y3: captures.next().unwrap().get(0).unwrap().as_str().parse().unwrap(), y3,
} }
}) })
.collect() .collect()