1
0
Fork 0

Some cleanup to deduplicate a bit

This commit is contained in:
Vivianne 2022-12-10 15:32:54 -08:00
parent b1f467d62b
commit 5f390df957

View file

@ -239,6 +239,15 @@ impl State {
}
}
fn from_lines(lines: &[String]) -> Result<Self, Box<dyn Error>> {
let mut state = State::new();
lines
.iter()
.flat_map(|l| line(l))
.try_for_each(|(_, l)| state.process(l))?;
Ok(state)
}
fn process(&mut self, line: Line) -> Result<(), Box<dyn Error>> {
match line {
Line::Command(cmd) => self.run(cmd),
@ -281,13 +290,7 @@ fn main() {
}
fn part1(lines: &[String]) {
let mut state = State::new();
lines
.iter()
.flat_map(|l| line(l))
.for_each(|(_, l)| state.process(l).unwrap());
let mut state = State::from_lines(lines).unwrap();
let full_size = state.filesystem.calculate_size();
dbg!(full_size);
@ -302,13 +305,7 @@ fn part1(lines: &[String]) {
}
fn part2(lines: &[String]) {
let mut state = State::new();
lines
.iter()
.flat_map(|l| line(l))
.for_each(|(_, l)| state.process(l).unwrap());
let mut state = State::from_lines(lines).unwrap();
let full_size = state.filesystem.calculate_size();
dbg!(full_size);
@ -319,7 +316,7 @@ fn part2(lines: &[String]) {
let over_by = full_size - MAX_USED;
println!(
"{}",
"Minimum dir to delete: {}",
state
.filesystem
.into_iter()