1
0
Fork 0

Day7 : Part One OK. Added list_dirs function. Fixed change_directory() logic. Added get_directories() and get_path() to calculate size. Added get_path().

This commit is contained in:
Yohan Boujon 2023-10-31 00:06:34 +01:00
parent ad5fd255b7
commit 0fce2aa521
3 changed files with 1098 additions and 24 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,25 +1,26 @@
use std::fs::read_to_string; use std::fs::read_to_string;
mod system; mod system;
use system::Disk;
fn decode(cmd: Vec<&str>) -> system::Disk { fn decode(cmd: Vec<&str>) -> Disk {
let mut index = 1; // ignoring the first cd / let mut index = 1; // ignoring the first cd /
let mut disk = system::Disk::new(); let mut disk = Disk::new();
while index < cmd.len() { while index < cmd.len() {
if cmd[index].contains("ls") { let mut info: Vec<&str> = cmd[index].split(' ').collect();
if info[1] == "ls" {
index += 1; index += 1;
while (index < cmd.len()) && !(cmd[index].contains("ls") || cmd[index].contains("cd")) { while (index < cmd.len()) && (!(cmd[index].as_bytes()[0] == ('$' as u8))) {
let info : Vec<&str> = cmd[index].split(' ').collect(); info = cmd[index].split(' ').collect();
match info[0] { match info[0] {
"dir" => disk.create_directory(info[1]), "dir" => disk.create_directory(info[1]),
&_ => disk.create_file(info[0].parse::<u64>().unwrap(),info[1]) &_ => disk.create_file(info[0].parse::<u64>().unwrap(), info[1]),
} }
index += 1; index += 1;
} }
} else if cmd[index].contains("cd") { } else if info[1] == "cd" {
let info : Vec<&str> = cmd[index].split(' ').collect();
match info[2] { match info[2] {
".." => disk.goto_parent(), ".." => disk.goto_parent(),
&_ => disk.change_directory(info[2]) &_ => disk.change_directory(info[2]),
} }
index += 1; index += 1;
} else { } else {
@ -29,8 +30,26 @@ fn decode(cmd: Vec<&str>) -> system::Disk {
disk disk
} }
fn list_dirs(disk: &Disk, max: u64) -> u64 {
let mut sum = 0;
for iter in disk
.get_directories()
.iter()
.filter(|dir| disk.get_size(dir) <= max)
{
println!(
"dir: {} with size: {}",
disk.get_path(iter),
disk.get_size(iter)
);
sum += disk.get_size(iter);
}
sum
}
fn main() { fn main() {
let file_content = read_to_string("exemple").unwrap(); let file_content = read_to_string("input").unwrap();
let final_disk = decode(file_content.split("\n").collect()); let final_disk = decode(file_content.split("\n").collect());
final_disk.print(); final_disk.print();
println!("Total sum: {}", list_dirs(&final_disk, 100000));
} }

View file

@ -1,10 +1,10 @@
#[derive(Debug)] #[derive(Clone)]
pub struct File { pub struct File {
size: u64, size: u64,
name: String, name: String,
} }
#[derive(Debug)] #[derive(Clone)]
pub struct Directory { pub struct Directory {
name: String, name: String,
index: usize, index: usize,
@ -14,6 +14,7 @@ pub struct Directory {
sublevel: u64, sublevel: u64,
} }
#[derive(Clone)]
pub struct Disk { pub struct Disk {
dir_list: Vec<Directory>, dir_list: Vec<Directory>,
selected_dir: usize, selected_dir: usize,
@ -37,6 +38,10 @@ impl Directory {
name: name.to_string(), name: name.to_string(),
}); });
} }
pub fn get_name(&self) -> &str {
&self.name
}
} }
impl Disk { impl Disk {
@ -65,18 +70,23 @@ impl Disk {
} }
pub fn change_directory(&mut self, name: &str) { pub fn change_directory(&mut self, name: &str) {
let found_index: usize; let mut found_indexes: Vec<usize> = Vec::new();
match self.dir_list.iter_mut().find(|dir| dir.name == name) { let found: Vec<&mut Directory> = self
None => { .dir_list
.iter_mut()
.filter(|dir| dir.name == name)
.collect();
if found.is_empty() {
println!("There is no such folder '{}' in the disk.", name); println!("There is no such folder '{}' in the disk.", name);
return; return;
} }
Some(found_dir) => found_index = found_dir.index, for dir in found.iter() {
found_indexes.push(dir.index)
} }
match self.dir_list[self.selected_dir] match self.dir_list[self.selected_dir]
.child .child
.iter() .iter()
.find(|&&index| index == found_index) .find(|&index| found_indexes.contains(index))
{ {
None => println!( None => println!(
"There is no such folder '{}' in the directory '{}'", "There is no such folder '{}' in the directory '{}'",
@ -108,4 +118,34 @@ impl Disk {
println!("{}\tFile: {}, size: {}", space, f.name, f.size); println!("{}\tFile: {}, size: {}", space, f.name, f.size);
} }
} }
pub fn get_path(&self, dir: &Directory) -> String {
let mut path = dir.name.to_string()+"/";
let mut index = dir.parent;
while index != 0 {
path += &(self.dir_list[index].name);
path += "/";
index = self.dir_list[index].parent;
}
path.chars().rev().collect()
}
pub fn get_directories(&self) -> Vec<&Directory> {
let mut dirvec: Vec<&Directory> = Vec::new();
for dir in self.dir_list.iter() {
dirvec.push(dir);
}
dirvec
}
pub fn get_size(&self, dir: &Directory) -> u64 {
let mut size = 0;
for f in &dir.files {
size += f.size;
}
for index in &dir.child {
size += self.get_size(&self.dir_list[*index]);
}
size
}
} }