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:
parent
ad5fd255b7
commit
0fce2aa521
3 changed files with 1098 additions and 24 deletions
1015
rust/day7/input
1015
rust/day7/input
File diff suppressed because it is too large
Load diff
|
@ -1,27 +1,28 @@
|
||||||
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();
|
||||||
index +=1;
|
if info[1] == "ls" {
|
||||||
while (index < cmd.len()) && !(cmd[index].contains("ls") || cmd[index].contains("cd")) {
|
index += 1;
|
||||||
let info : Vec<&str> = cmd[index].split(' ').collect();
|
while (index < cmd.len()) && (!(cmd[index].as_bytes()[0] == ('$' as u8))) {
|
||||||
|
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 {
|
||||||
index += 1;
|
index += 1;
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
println!("There is no such folder '{}' in the disk.", name);
|
.iter_mut()
|
||||||
return;
|
.filter(|dir| dir.name == name)
|
||||||
}
|
.collect();
|
||||||
Some(found_dir) => found_index = found_dir.index,
|
if found.is_empty() {
|
||||||
|
println!("There is no such folder '{}' in the disk.", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue