78 lines
2.2 KiB
Rust
78 lines
2.2 KiB
Rust
use std::collections::HashMap;
|
|
use base::read_file;
|
|
|
|
fn generate_parent_paths(vec: &Vec<String>) -> Vec<String> {
|
|
let mut parent = "/".to_owned();
|
|
let mut result = vec![];
|
|
|
|
result.push(parent.clone());
|
|
for x in vec {
|
|
parent = parent + x + "/";
|
|
result.push(parent.clone());
|
|
}
|
|
result
|
|
}
|
|
|
|
fn main() {
|
|
let lines = read_file("day7.txt");
|
|
|
|
let mut current_dir = vec![];
|
|
let mut sizes = HashMap::new();
|
|
for output in lines {
|
|
if output.starts_with("$ cd") {
|
|
let mut output = output.split("$ cd");
|
|
output.next();
|
|
let sub_folder = output.next().unwrap().trim().to_owned();
|
|
if sub_folder != "/" {
|
|
if sub_folder == ".." {
|
|
current_dir.remove(current_dir.len() - 1);
|
|
} else {
|
|
current_dir.push(sub_folder);
|
|
}
|
|
}
|
|
} else if output.starts_with("$ ls") { } else {
|
|
if output.starts_with("dir") { } else {
|
|
let mut split = output.split_whitespace();
|
|
let size = split.next().unwrap().parse::<usize>().unwrap();
|
|
for parent in generate_parent_paths(¤t_dir) {
|
|
*sizes.entry(parent).or_insert_with(||0) += size;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut size_counter = 0;
|
|
|
|
for (k,v) in &sizes {
|
|
if *v <= 100000 {
|
|
size_counter += v;
|
|
}
|
|
}
|
|
|
|
println!("Sizes of all smaller 100000: {size_counter}");
|
|
|
|
let space_left = 70000000 - sizes["/"];
|
|
let space_req = 30000000 - space_left;
|
|
println!("Available space: {space_left}");
|
|
println!("Space required: {space_req}");
|
|
|
|
let mut found: Option<(usize, String)>= None;
|
|
for (k, v) in &sizes {
|
|
if *v >= space_req {
|
|
match &found {
|
|
None => {
|
|
found = Some((*v, k.clone()));
|
|
}
|
|
Some((size, _)) => {
|
|
if *v < *size {
|
|
found = Some((*v, k.clone()))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some((size, name)) = found {
|
|
println!("Would remove directory {name} with size {size}");
|
|
}
|
|
} |