diff --git a/2022/day7/rust/Cargo.toml b/2022/day7/rust/Cargo.toml new file mode 100644 index 0000000..c95f3e2 --- /dev/null +++ b/2022/day7/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust_2022_07" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +aoc = { path = "../../../common/rust" } diff --git a/2022/day7/rust/src/main.rs b/2022/day7/rust/src/main.rs new file mode 100644 index 0000000..305a951 --- /dev/null +++ b/2022/day7/rust/src/main.rs @@ -0,0 +1,76 @@ +#![warn(clippy::pedantic)] + +use std::{ + collections::HashMap, + hash::Hash, + io::{stdin, Read}, + path::{Path, PathBuf}, +}; + +use aoc::*; + +fn main() { + let mut data = String::new(); + stdin().read_to_string(&mut data).unwrap(); + + let mut cwd = PathBuf::new(); + cwd.push("/"); + let mut dirs: HashMap = HashMap::new(); + + for line in data.lines() { + if let Some(command) = line.strip_prefix("$ ") { + let mut args = command.split_whitespace(); + let command = args.next().unwrap(); + match command { + "ls" => { + dirs.remove(&cwd); + } + "cd" => { + let dir = args.next().unwrap(); + match dir { + ".." => assert!(cwd.pop()), + path => cwd.push(path), + } + } + _ => panic!("unknown command {command}"), + } + } else { + let (size, _name) = line.split_once(' ').unwrap(); + if size == "dir" { + // skip + } else { + let size: usize = size.parse().unwrap(); + *dirs.entry(cwd.clone()).or_default() += size; + } + } + } + + let mut dirs_recursive: HashMap<&Path, usize> = HashMap::new(); + for (dir, size) in &dirs { + for parent in std::iter::successors(Some(&**dir), |dir| dir.parent()) { + *dirs_recursive.entry(parent).or_default() += size; + } + } + + println!( + "{}", + dirs_recursive + .values() + .filter(|&&s| s <= 100_000) + .sum::() + ); + + const TOTAL_SPACE: usize = 70_000_000; + const REQUIRED_SPACE: usize = 30_000_000; + + let unused = TOTAL_SPACE - dirs_recursive[&*PathBuf::from("/")]; + let to_free = REQUIRED_SPACE - unused; + let mut sizes: Vec<_> = dirs_recursive.values().copied().collect(); + sizes.sort_unstable(); + let size = match sizes.binary_search(&to_free) { + Ok(i) => sizes[i], + Err(larger) => sizes[larger], + }; + + println!("{}", size); +} diff --git a/Cargo.lock b/Cargo.lock index fc85520..df24bd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,6 +510,13 @@ dependencies = [ "aoc", ] +[[package]] +name = "rust_2022_07" +version = "0.1.0" +dependencies = [ + "aoc", +] + [[package]] name = "ryu" version = "1.0.11" diff --git a/Cargo.toml b/Cargo.toml index a7a3b01..2581d97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,5 @@ members = [ "2022/day4/rust", "2022/day5/rust", "2022/day6/rust", + "2022/day7/rust", ]