feat: implement get_args :))))

This commit is contained in:
max_richter 2024-04-13 12:47:39 +02:00
parent c45c598ba9
commit d262d4b9fd

View File

@ -1,94 +1,95 @@
pub fn get_arg<'a>(index: i32, args: &'a Vec<i32>) -> Option<&'a [i32]> { pub fn get_args<'a>(args: &'a Vec<i32>) -> Vec<&[i32]> {
let mut arg_index = -1; let mut idx: usize = 0;
let mut i: usize = 0; let mut depth = -1;
let mut depth = 0;
let mut arg_start_index = 0; let mut arg_start_index = 2;
let mut next_bracket_index = 0; let mut next_bracket_index = 0;
let mut last_bracket_index = 0;
while i < args.len() { let mut out_args: Vec<&[i32]> = Vec::new();
println!("------");
let is_bracket = i == next_bracket_index; let length = args.len();
let value = args[i];
while idx < length {
let is_bracket = idx == next_bracket_index;
let value = args[idx];
// if we are at a bracket // if we are at a bracket
if is_bracket { if is_bracket {
next_bracket_index = 1 + i + args[i + 1] as usize; // if we are at the end of the args
// if is opening if idx >= length - 1 {
break;
}
last_bracket_index = next_bracket_index;
next_bracket_index = 1 + idx + args[idx + 1] as usize;
if value == 0 { if value == 0 {
if i != 0 {
depth += 1; depth += 1;
} else if value == 1 {
depth -= 1;
} }
if depth == 0 { if depth == 0 {
i += 2; // skip over the bracket encoding
arg_index += 1; idx += 2;
} else { } else {
i = next_bracket_index; // skip to the next bracket if we are at depth > 0
} idx = next_bracket_index - 1;
// if closing
} else if value == 1 {
depth -= 1;
if depth > 1 {
i = next_bracket_index;
} else {
i += 2;
}
} }
continue; continue;
} }
arg_index += 1; // this is at the end of args where normally multiple ] are encoded
if depth < 0 {
println!( break;
"i: {}, next: {}, v: {}, ai: {}",
i, next_bracket_index, args[i], arg_index
);
println!("is_bracket: {}", is_bracket);
println!("depth: {}", depth);
println!("arg_start_index: {}", arg_start_index);
if arg_index == index {
println!("------");
return Some(&args[arg_start_index..=i]);
} }
i += 1; // remove starting bracket from single numbers
if idx - arg_start_index < 3 && last_bracket_index == arg_start_index {
arg_start_index += 2;
} }
None out_args.push(&args[arg_start_index..=idx]);
idx += 1;
arg_start_index = idx;
} }
fn main() { return out_args;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example_input_a() {
let input_a = vec![0, 4, 1, 2, 3, 0, 7, 1, 2, 4, 2, 4, 1, 1, 1, 1]; let input_a = vec![0, 4, 1, 2, 3, 0, 7, 1, 2, 4, 2, 4, 1, 1, 1, 1];
// -> [1, 2, 3, [1, 2, 4, 2, 4]] // -> [1, 2, 3, [1, 2, 4, 2, 4]]
// 1: [1]
// 2: [2]
// 3: [3]
// 4: [1,2,4,2,4]
let input_b = vec![0, 2, 1, 0, 4, 4, 2, 4, 1, 2, 2, 0, 3, 2, 3, 1, 1, 1, 1]; let args = get_args(&input_a);
// -> [1,[4,2,4], 2, [2,3]];
// 1: [1]
// 2: [4,2,4]
// 3: [2]
// 4: [2,3]
let input = input_b; assert_eq!(args.len(), 4);
assert_eq!(args[0], [1]);
//let first_arg = get_arg(0, &input); assert_eq!(args[1], [2]);
//println!("-----> FIRST: {:?}", first_arg); assert_eq!(args[2], [3]);
assert_eq!(args[3], [0, 7, 1, 2, 4, 2, 4, 1]);
//let second_arg = get_arg(1, &input);
//println!("-----> SECOND: {:?}", second_arg);
//let third_arg = get_arg(2, &input);
//println!("-----> THIRD: {:?}", third_arg);
let fourth_arg = get_arg(3, &input);
println!("-----> FOURTH: {:?}", fourth_arg);
} }
#[test]
fn test_example_input_b() {
let input_b = vec![0, 3, 7, 1, 0, 4, 4, 2, 4, 1, 2, 2, 0, 3, 2, 3, 1, 1, 1, 1];
// -> [1,[4,2,4], 2, [2,3]]
let args = get_args(&input_b);
assert_eq!(args.len(), 5);
assert_eq!(args[0], [7]);
assert_eq!(args[1], [1]);
assert_eq!(args[2], [0, 4, 4, 2, 4]);
assert_eq!(args[3], [2]);
assert_eq!(args[4], [0, 3, 2, 3]);
}
}