From 1156bc2e75ad55ed47bce6f6ea924ae78bb8ddd1 Mon Sep 17 00:00:00 2001 From: jazzpi Date: Wed, 14 Dec 2022 13:32:05 +0100 Subject: [PATCH] Day 7, puzzle 2 --- src/bin/d7p2.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/bin/d7p2.rs diff --git a/src/bin/d7p2.rs b/src/bin/d7p2.rs new file mode 100644 index 0000000..414f479 --- /dev/null +++ b/src/bin/d7p2.rs @@ -0,0 +1,26 @@ +use aoc22::{day7, util}; + +const TOTAL_SPACE: usize = 70000000; +const REQUIRED_SPACE: usize = 30000000; + +pub fn main() { + let root = day7::parse_cmdline(&util::parse_input()); + + let r = &*root.borrow(); + let free_space = TOTAL_SPACE - r.size(); + assert!(free_space < REQUIRED_SPACE); + let required_deletion = REQUIRED_SPACE - free_space; + + let mut min_sufficient = usize::MAX; + for child in root.borrow().all_children() { + let c = &*child.borrow(); + if let day7::Node::Dir { .. } = c { + let size = c.size(); + if size >= required_deletion && size <= min_sufficient { + min_sufficient = size; + } + } + } + + println!("Size of min sufficient dir is {}", min_sufficient); +}