Simplify match

This commit is contained in:
kageru 2021-12-10 22:51:24 +01:00
parent 6d9fbe0edf
commit aa33a74bc7
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -23,15 +23,18 @@ fn solve(input: &str) -> (usize, usize) {
fn is_well_formed<'a>(line: &str, stack: &'a mut Vec<u8>) -> Result<&'a mut Vec<u8>, usize> {
for c in line.bytes() {
match (stack.last(), c) {
(_, b'(' | b'[' | b'<' | b'{') => stack.push(c),
(Some(b'('), b')') | (Some(b'['), b']') | (Some(b'{'), b'}') | (Some(b'<'), b'>') => {
match c {
b'(' | b'[' | b'<' | b'{' => stack.push(c),
b']' | b'}' | b'>' if stack.last().unwrap() + 2 == c => {
stack.pop();
}
(_, b')') => return Err(3),
(_, b']') => return Err(57),
(_, b'}') => return Err(1197),
(_, b'>') => return Err(25137),
b')' if stack.last().unwrap() == &b'(' => {
stack.pop();
}
b')' => return Err(3),
b']' => return Err(57),
b'}' => return Err(1197),
b'>' => return Err(25137),
_ => unreachable!(),
}
}