From 5f390df957cc7907de2ad61ae8f2f2c7db986b20 Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Sat, 10 Dec 2022 15:32:54 -0800 Subject: [PATCH] Some cleanup to deduplicate a bit --- src/bin/p07.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/bin/p07.rs b/src/bin/p07.rs index b9b75dd..b2e87f2 100644 --- a/src/bin/p07.rs +++ b/src/bin/p07.rs @@ -239,6 +239,15 @@ impl State { } } + fn from_lines(lines: &[String]) -> Result> { + 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> { 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()