Add day 7
This commit is contained in:
parent
41b1b887b3
commit
7081f708c3
@ -33,4 +33,8 @@ path = "src/day5/main.rs"
|
||||
name = "day6"
|
||||
path = "src/day6/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day7"
|
||||
path = "src/day7/main.rs"
|
||||
|
||||
[dependencies]
|
||||
|
23
day7_test.txt
Normal file
23
day7_test.txt
Normal file
@ -0,0 +1,23 @@
|
||||
$ cd /
|
||||
$ ls
|
||||
dir a
|
||||
14848514 b.txt
|
||||
8504156 c.dat
|
||||
dir d
|
||||
$ cd a
|
||||
$ ls
|
||||
dir e
|
||||
29116 f
|
||||
2557 g
|
||||
62596 h.lst
|
||||
$ cd e
|
||||
$ ls
|
||||
584 i
|
||||
$ cd ..
|
||||
$ cd ..
|
||||
$ cd d
|
||||
$ ls
|
||||
4060174 j
|
||||
8033020 d.log
|
||||
5626152 d.ext
|
||||
7214296 k
|
78
src/day7/main.rs
Normal file
78
src/day7/main.rs
Normal file
@ -0,0 +1,78 @@
|
||||
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}");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user