simplify rust code somewhat

This commit is contained in:
David 2022-12-07 22:16:15 -05:00
parent 5809d442da
commit 5426357cd7

View File

@ -42,20 +42,17 @@ fn part_one() {
if f < 100000 { if f < 100000 {
sum += f sum += f
} }
let mut p = stack.pop().unwrap(); let last_on_stack = stack.len() - 1;
p += f; stack[last_on_stack] += f;
stack.push(p);
} }
s if s.starts_with("$ cd ") => { s if s.starts_with("$ cd ") => {
stack.push(0); stack.push(0);
} }
s => { s => {
// otherwise it must be a file // otherwise it must be a file
let mut f = stack.pop().unwrap();
let mut sp = s.split(" "); let mut sp = s.split(" ");
let size = sp.next().unwrap().parse::<u32>().unwrap(); let last_on_stack = stack.len() - 1;
f += size; stack[last_on_stack] += sp.next().unwrap().parse::<u32>().unwrap();
stack.push(f);
} }
}; };
} }
@ -80,20 +77,17 @@ fn part_two() {
if f > space_to_free && f < to_delete { if f > space_to_free && f < to_delete {
to_delete = f to_delete = f
} }
let mut p = stack.pop().unwrap(); let last_on_stack = stack.len() - 1;
p += f; stack[last_on_stack] += f;
stack.push(p);
} }
s if s.starts_with("$ cd ") => { s if s.starts_with("$ cd ") => {
stack.push(0); stack.push(0);
} }
s => { s => {
// otherwise it must be a file // otherwise it must be a file
let mut f = stack.pop().unwrap();
let mut sp = s.split(" "); let mut sp = s.split(" ");
let size = sp.next().unwrap().parse::<u32>().unwrap(); let last_on_stack = stack.len() - 1;
f += size; stack[last_on_stack] += sp.next().unwrap().parse::<u32>().unwrap();
stack.push(f);
} }
}; };
} }