try number who knows for day07.
This commit is contained in:
		
							parent
							
								
									70a96b79a2
								
							
						
					
					
						commit
						824321c660
					
				
					 4 changed files with 55 additions and 105 deletions
				
			
		| 
						 | 
					@ -1,82 +1,20 @@
 | 
				
			||||||
use std::{
 | 
					use std::{
 | 
				
			||||||
    cell::RefCell,
 | 
					    cell::RefCell,
 | 
				
			||||||
    collections::{hash_map, HashMap},
 | 
					 | 
				
			||||||
    ops::Deref,
 | 
					    ops::Deref,
 | 
				
			||||||
    rc::{Rc, Weak},
 | 
					    rc::{Rc, Weak},
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub type WeakDirRef = Weak<RefCell<Dir>>;
 | 
					use thiserror::Error;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub trait FileLike {
 | 
					#[derive(Error, Debug)]
 | 
				
			||||||
    fn get_parent(&self) -> Option<DirRef>;
 | 
					pub enum FileTreeError {
 | 
				
			||||||
    fn get_name(&self) -> String;
 | 
					    #[error("Directory operation on file")]
 | 
				
			||||||
 | 
					    IsFile,
 | 
				
			||||||
 | 
					    #[error("File operation on directory")]
 | 
				
			||||||
 | 
					    IsDir,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					type WeakNodeRef = Weak<RefCell<Node>>;
 | 
				
			||||||
pub struct DirRef(Rc<RefCell<Dir>>);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl Deref for DirRef {
 | 
					 | 
				
			||||||
    type Target = Rc<RefCell<Dir>>;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    fn deref(&self) -> &Self::Target {
 | 
					 | 
				
			||||||
        &self.0
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl DirRef {
 | 
					 | 
				
			||||||
    pub fn add_child_node(&mut self, mut node: Node) -> NodeRef {
 | 
					 | 
				
			||||||
        node.change_parent(self);
 | 
					 | 
				
			||||||
        let node_ref = NodeRef(Rc::new(RefCell::new(node)));
 | 
					 | 
				
			||||||
        Rc::clone(self)
 | 
					 | 
				
			||||||
            .borrow_mut()
 | 
					 | 
				
			||||||
            .children
 | 
					 | 
				
			||||||
            .push(NodeRef(Rc::clone(&node_ref)));
 | 
					 | 
				
			||||||
        node_ref
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn add_child_dir(&mut self, mut dir: Dir) -> DirRef {
 | 
					 | 
				
			||||||
        dir.parent = Some(Rc::downgrade(self));
 | 
					 | 
				
			||||||
        let dir_ref = DirRef(Rc::new(RefCell::new(dir)));
 | 
					 | 
				
			||||||
        Rc::clone(self).borrow_mut().children.push(
 | 
					 | 
				
			||||||
            Node::Dir(())
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
        todo!()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[derive(Debug)]
 | 
					 | 
				
			||||||
pub struct Dir {
 | 
					 | 
				
			||||||
    name: String,
 | 
					 | 
				
			||||||
    parent: Option<WeakDirRef>,
 | 
					 | 
				
			||||||
    children: Vec<NodeRef>,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl Dir {
 | 
					 | 
				
			||||||
    pub fn get_total_size(&self) -> usize {
 | 
					 | 
				
			||||||
        self.children
 | 
					 | 
				
			||||||
            .iter()
 | 
					 | 
				
			||||||
            .map(|n| -> usize {
 | 
					 | 
				
			||||||
                match Rc::clone(n).borrow().deref() {
 | 
					 | 
				
			||||||
                    Node::Dir(d) => d.get_total_size(),
 | 
					 | 
				
			||||||
                    Node::File(f) => f.size,
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
            .sum()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn get_children(&self) -> impl Iterator<Item = NodeRef> + '_ {
 | 
					 | 
				
			||||||
        self.children.iter().map(|n| NodeRef(Rc::clone(n)))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl FileLike for Dir {
 | 
					 | 
				
			||||||
    fn get_parent(&self) -> Option<DirRef> {
 | 
					 | 
				
			||||||
        Some(DirRef(self.parent.as_ref()?.upgrade()?))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    fn get_name(&self) -> String {
 | 
					 | 
				
			||||||
        self.name.clone()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub struct NodeRef(Rc<RefCell<Node>>);
 | 
					pub struct NodeRef(Rc<RefCell<Node>>);
 | 
				
			||||||
| 
						 | 
					@ -89,48 +27,60 @@ impl Deref for NodeRef {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl From<Rc<RefCell<Node>>> for NodeRef {
 | 
				
			||||||
 | 
					    fn from(value: Rc<RefCell<Node>>) -> Self {
 | 
				
			||||||
 | 
					        NodeRef(value)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl From<NodeRef> for Rc<RefCell<Node>> {
 | 
				
			||||||
 | 
					    fn from(value: NodeRef) -> Self {
 | 
				
			||||||
 | 
					        value.0
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub enum Node {
 | 
					pub struct Node {
 | 
				
			||||||
    Dir(Dir),
 | 
					    pub name: String,
 | 
				
			||||||
    File(File),
 | 
					    parent: Option<WeakNodeRef>,
 | 
				
			||||||
 | 
					    contents: Contents,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Node {
 | 
					impl Node {
 | 
				
			||||||
    fn change_parent(&mut self, new_parent: &DirRef) {
 | 
					    fn set_parent(&mut self, newparent: &NodeRef) {
 | 
				
			||||||
        match self {
 | 
					        self.parent = Some(Rc::downgrade(newparent));
 | 
				
			||||||
            Node::Dir(d) => d.parent = Some(Rc::downgrade(new_parent)),
 | 
					    }
 | 
				
			||||||
            Node::File(f) => f.parent = Rc::downgrade(new_parent),
 | 
					    pub fn get_parent(&self) -> Option<NodeRef> {
 | 
				
			||||||
 | 
					        match &self.parent{
 | 
				
			||||||
 | 
					            Some(w) => Some(NodeRef(w.clone().upgrade()?)),
 | 
				
			||||||
 | 
					            None => None,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					    pub fn get_size(&self) -> Result<usize, FileTreeError> {
 | 
				
			||||||
 | 
					        match self.contents {
 | 
				
			||||||
impl FileLike for Node {
 | 
					            Contents::Size(s) => Ok(s),
 | 
				
			||||||
    fn get_parent(&self) -> Option<DirRef> {
 | 
					            Contents::Children(_) => Err(FileTreeError::IsDir),
 | 
				
			||||||
        match self {
 | 
					 | 
				
			||||||
            Node::Dir(d) => d.get_parent(),
 | 
					 | 
				
			||||||
            Node::File(f) => f.get_parent(),
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    fn get_name(&self) -> String {
 | 
					    pub fn set_size(&mut self, size: usize) -> Result<(), FileTreeError> {
 | 
				
			||||||
        match self {
 | 
					        match self.contents {
 | 
				
			||||||
            Node::Dir(d) => d.get_name(),
 | 
					            Contents::Size(ref mut s) => {
 | 
				
			||||||
            Node::File(f) => f.get_name(),
 | 
					                *s = size;
 | 
				
			||||||
 | 
					                Ok(())
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            Contents::Children(_) => Err(FileTreeError::IsDir),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    pub fn get_children(&self) -> Result<impl Iterator<Item = NodeRef> + '_, FileTreeError> {
 | 
				
			||||||
 | 
					        match &self.contents {
 | 
				
			||||||
 | 
					            Contents::Size(_) => Err(FileTreeError::IsFile),
 | 
				
			||||||
 | 
					            Contents::Children(c) => Ok(c.iter().map(|n| NodeRef(Rc::clone(n)))),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub struct File {
 | 
					pub enum Contents {
 | 
				
			||||||
    name: String,
 | 
					    Size(usize),
 | 
				
			||||||
    parent: WeakDirRef,
 | 
					    Children(Vec<NodeRef>),
 | 
				
			||||||
    pub size: usize,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl FileLike for File {
 | 
					 | 
				
			||||||
    fn get_parent(&self) -> Option<DirRef> {
 | 
					 | 
				
			||||||
        Some(DirRef(self.parent.upgrade()?))
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    fn get_name(&self) -> String {
 | 
					 | 
				
			||||||
        self.name.clone()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,11 +73,11 @@ fn parse_ls(lines: &mut std::iter::Peekable<std::str::Lines<'_>>) -> Vec<LsEntry
 | 
				
			||||||
    ret
 | 
					    ret
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn parse(input: &str) -> Dir {
 | 
					pub fn parse(input: &str) -> Node {
 | 
				
			||||||
    unimplemented!()
 | 
					    unimplemented!()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn commands_to_tree(input: Vec<Command>) -> Dir {
 | 
					pub fn commands_to_tree(input: Vec<Command>) -> Node {
 | 
				
			||||||
    unimplemented!()
 | 
					    unimplemented!()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
#![allow(unused)]
 | 
					#![allow(dead_code)]
 | 
				
			||||||
mod part1;
 | 
					mod part1;
 | 
				
			||||||
mod part2;
 | 
					mod part2;
 | 
				
			||||||
mod parser;
 | 
					mod parser;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
#![allow(unused)]
 | 
					#![allow(dead_code)]
 | 
				
			||||||
mod part1;
 | 
					mod part1;
 | 
				
			||||||
mod part2;
 | 
					mod part2;
 | 
				
			||||||
mod utilities;
 | 
					mod utilities;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue