Skip to content
Snippets Groups Projects
Commit a91bfb2c authored by Falk Rehse's avatar Falk Rehse
Browse files

Simplify

parent 19a9ea6a
Branches
No related tags found
1 merge request!1Day 7
use std::rc::Rc;
use crate::day_7_1::command::Command;
use crate::day_7_1::node::Node::File;
#[derive(Debug, Clone)]
pub enum FileTree {
File { name: String, size: u32 },
Directory { name: String, files: Vec<FileTree> },
File {
name: String,
parent: Option<Rc<FileTree>>,
size: u32,
},
Directory {
name: String,
parent: Option<Rc<FileTree>>,
files: Vec<FileTree>,
},
}
impl FileTree {
pub fn new() -> FileTree {
Self::Directory {
name: "".to_string(),
parent: None,
files: Vec::new(),
}
}
pub fn fill_from_commands(&mut self, commands: &Vec<Command>) {
let mut current_path = "/".to_string();
pub fn from_commands(commands: &Vec<Command>) -> FileTree {
let mut tree = Rc::new(Self::new());
for command in commands {
match command {
Command::ChangeDir { path } => {
current_path = self.change_path(&current_path, &path);
tree = Self::change_path(tree, &path);
}
Command::List { files } => {
let tree = self.create_path(&current_path);
for file in files {
if let File { name, size } = file {
tree.add_file(name, size);
}
// tree.add_file(Rc::clone(&mut tree), name, size);
}
}
}
}
}
fn change_path<'a>(&self, current_path: &'a str, cd_path: &'a str) -> String {
if cd_path.starts_with("/") {
cd_path.to_string()
} else if cd_path == ".." {
let mut path: Vec<&str> = current_path.split('/').collect();
let current_dir = path.pop().unwrap();
(*tree).clone()
}
current_path
.strip_suffix(&format!("/{}", current_dir))
.unwrap()
.to_string()
fn change_path(tree: Rc<FileTree>, path: &str) -> Rc<FileTree>{
if path == "/" {
Rc::clone(&tree)
} else if path == ".." {
Rc::clone(&tree.get_parent().as_ref().unwrap())
} else {
format!("{}/{}", current_path, cd_path)
Self::create_directory(tree, path)
}
}
fn create_path(&mut self, path: &str) -> &mut FileTree {
let mut tree = self;
let segments = path.split('/');
for segment in segments {
let new_tree = if let Self::Directory { files, .. } = tree {
let new_tree = files.iter_mut().find(|tree| match tree {
Self::Directory { name, .. } => name == segment,
_ => false,
});
new_tree
} else {
panic!("Expected a directory!");
};
tree = if let Some(new_tree) = new_tree {
new_tree
} else {
panic!();
fn create_directory(mut tree: Rc<FileTree>, path: &str) -> Rc<FileTree> {
let file_tree: &mut FileTree = &mut tree;
/* if let Self::Directory { files, .. } = tree {
let mut new_dir = Self::Directory {
name: segment.to_string(),
if let Self::Directory { files, .. } = file_tree {
let mut directory = Self::Directory {
name: path.to_string(),
parent: Some(Rc::clone(&tree)),
files: Vec::new(),
};
files.push(new_dir);
&mut new_dir
files.push(directory);
Rc::new(directory)
} else {
panic!("Expected a directory!");
} */
}
}
tree
}
fn add_file(&mut self, name: &str, size: &u32) {
fn add_file(&mut self, tree: Rc<FileTree>, name: &str, size: &u32) {
if let Self::Directory { files, .. } = self {
files.push(Self::File {
name: name.to_string(),
parent: Some(tree),
size: size.clone(),
})
}
}
fn get_parent(&mut self) -> &mut Option<Rc<FileTree>> {
match self {
Self::Directory { parent, .. } => parent,
Self::File { parent, .. } => parent,
}
}
}
......@@ -14,9 +14,7 @@ pub fn solve_from_str(input: &str) -> u32 {
let commands = commands.map(|command| Command::from_str(command)).collect();
let mut file_tree = FileTree::new();
file_tree.fill_from_commands(&commands);
let file_tree = FileTree::from_commands(&commands);
solve(file_tree)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment