Rust solution:
fn completer(bracket: char) -> char {
match bracket {
'[' => ']',
'{' => '}',
'(' => ')',
')' => '(',
'}' => '{',
']' => '[',
_ => unreachable!(),
}
}
fn rec(v: &mut Vec, stack: &mut Vec, depth: usize) {
// If we have more characters in the stack to complete
// than the space avaiable, bail!
if stack.len() > depth {
return;
}
if depth == 0 {
let output: String = v.iter().collect();
println!("{}", output);
return;
}
for b in &['[', '{', '('] {
v.push(*b);
stack.push(*b);
rec(v, stack, depth - 1);
stack.pop();
v.pop();
}
if let Some(c) = stack.pop() {
v.push(completer(c));
rec(v, stack, depth - 1);
stack.push(c);
v.pop();
}
}
fn main() {
let depth: usize = std::env::args().nth(1).unwrap().parse().unwrap();
let mut v = Vec::with_capacity(depth);
let mut s = Vec::with_capacity(depth / 2);
rec(&mut v, &mut s, depth);
}
Hope you enjoy this!
Edit: Lemmy makes the usize
in angle brackets disappear after parse::
. Idk what to do.
Edit: I kinda fixed it by assigning the type as a part of the variable definition.
Note to self: the angle bracket problem persists nonetheless.