Skip to content

Commit

Permalink
2022_07: rust solution(part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 10, 2022
1 parent f76c1b0 commit bcdc858
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 85 deletions.
25 changes: 25 additions & 0 deletions 2022/day_7/rust/src/file_system/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,29 @@ impl<'a> Directory<'a> {

return size;
}

pub fn get_smallest_folder_to_delete(&self, space_to_free: u32) -> Option<u32> {
let mut result = None;

let curr_dir_size = self.get_dir_size();
if curr_dir_size >= space_to_free {
result = Some(curr_dir_size);
}

if self.dirs.len() > 0 && result != None {
let min_child = self
.dirs
.iter()
.map(|d| d.get_smallest_folder_to_delete(space_to_free))
.filter(|size| size.is_some())
.map(|size| size.unwrap())
.min();

if min_child != None && min_child.unwrap() < result.unwrap() {
return min_child;
}
}

return result;
}
}
16 changes: 15 additions & 1 deletion 2022/day_7/rust/src/file_system/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ pub struct ListItems<'a> {

#[derive(Debug)]
pub struct FileSystem<'a> {
total_memory: u32,
root_dir: Directory<'a>,
pub cwd: *mut Directory<'a>,
}

impl<'a> FileSystem<'a> {
pub fn new() -> Self {
pub fn new(total_memory: u32) -> Self {
let mut fs = Self {
total_memory,
root_dir: Directory::new("", None),
cwd: null_mut(),
};
Expand Down Expand Up @@ -72,7 +74,19 @@ impl<'a> FileSystem<'a> {
}
}

pub fn get_unused_space(&self) -> u32 {
self.total_memory - self.get_system_size()
}

pub fn get_system_size(&self) -> u32 {
self.root_dir.get_dir_size()
}

pub fn get_system_size_max(&self, max_size: u32) -> u32 {
self.root_dir.get_dir_size_max(max_size)
}

pub fn get_smallest_folder_to_delete(&self, space_to_free: u32) -> Option<u32> {
self.root_dir.get_smallest_folder_to_delete(space_to_free)
}
}
97 changes: 13 additions & 84 deletions 2022/day_7/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,96 +1,25 @@
mod file_system;
mod parser;

use core::panic;

use file_system::{ChangeDirectory, Command, Directory, File, FileSystem, ListItems};

struct FileSystemParser<'a> {
fs: FileSystem<'a>,
}

impl<'a> FileSystemParser<'a> {
fn new(fs: FileSystem<'a>) -> Self {
Self { fs }
}

fn parse_commands_string(&self, string: &'a str) -> Vec<Command<'a>> {
let commands: Vec<Command> = string
.split("$ ")
.skip(1)
.map(|command_str| {
let command_vector = command_str.trim().split("\n").collect::<Vec<&str>>();

if command_vector[0] == "ls" {
let ls_output = command_vector.iter().skip(1);
let mut ls = ListItems {
dirs: Vec::new(),
files: Vec::new(),
};

for &line in ls_output {
let mut iter = line.split(" ");

match iter.next() {
Some("dir") => {
ls.dirs.push(iter.next().unwrap());
}
Some(num) => {
ls.files
.push(File::new(iter.next().unwrap(), num.parse::<u32>().unwrap()));
}
None => panic!(),
}
}

return Command::LS(ls);
}

let first_line_parts = command_vector[0].split(" ").collect::<Vec<&str>>();

if first_line_parts[0] == "cd" {
let cd_command = match first_line_parts[1] {
".." => ChangeDirectory::Up,
"/" => ChangeDirectory::Root,
path => ChangeDirectory::Down(path),
};
return Command::CD(cd_command);
}

panic!("Invalid command");
})
.collect();

return commands;
}

fn apply_command(&mut self, command: Command<'a>) {
match command {
Command::CD(cd) => {
self.fs.chage_dir(cd);
}
Command::LS(ls) => {
for dir_name in ls.dirs {
self.fs.make_dir(dir_name);
}

for file in ls.files {
unsafe {
self.fs.cwd.as_mut().unwrap().add_file(file);
}
}
}
}
}
}
use file_system::FileSystem;
use parser::FileSystemParser;

fn main() {
let mut fs_parser = FileSystemParser::new(FileSystem::new());
let mut fs_parser = FileSystemParser::new(FileSystem::new(70_000_000));

let commands = fs_parser.parse_commands_string(include_str!("../../input.txt"));

for command in commands {
fs_parser.apply_command(command);
}

println!("{:#?}", fs_parser.fs.get_system_size_max(100_000));
let target_unused_space: u32 = 30_000_000;
let target_free_space = target_unused_space - fs_parser.fs.get_unused_space();

println!(
"{:?}",
fs_parser
.fs
.get_smallest_folder_to_delete(target_free_space)
);
}
80 changes: 80 additions & 0 deletions 2022/day_7/rust/src/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::file_system::{ChangeDirectory, Command, File, FileSystem, ListItems};

pub struct FileSystemParser<'a> {
pub fs: FileSystem<'a>,
}

impl<'a> FileSystemParser<'a> {
pub fn new(fs: FileSystem<'a>) -> Self {
Self { fs }
}

pub fn parse_commands_string(&self, string: &'a str) -> Vec<Command<'a>> {
let commands: Vec<Command> = string
.split("$ ")
.skip(1)
.map(|command_str| {
let command_vector = command_str.trim().split("\n").collect::<Vec<&str>>();

if command_vector[0] == "ls" {
let ls_output = command_vector.iter().skip(1);
let mut ls = ListItems {
dirs: Vec::new(),
files: Vec::new(),
};

for &line in ls_output {
let mut iter = line.split(" ");

match iter.next() {
Some("dir") => {
ls.dirs.push(iter.next().unwrap());
}
Some(num) => {
ls.files
.push(File::new(iter.next().unwrap(), num.parse::<u32>().unwrap()));
}
None => panic!(),
}
}

return Command::LS(ls);
}

let first_line_parts = command_vector[0].split(" ").collect::<Vec<&str>>();

if first_line_parts[0] == "cd" {
let cd_command = match first_line_parts[1] {
".." => ChangeDirectory::Up,
"/" => ChangeDirectory::Root,
path => ChangeDirectory::Down(path),
};
return Command::CD(cd_command);
}

panic!("Invalid command");
})
.collect();

return commands;
}

pub fn apply_command(&mut self, command: Command<'a>) {
match command {
Command::CD(cd) => {
self.fs.chage_dir(cd);
}
Command::LS(ls) => {
for dir_name in ls.dirs {
self.fs.make_dir(dir_name);
}

for file in ls.files {
unsafe {
self.fs.cwd.as_mut().unwrap().add_file(file);
}
}
}
}
}
}

0 comments on commit bcdc858

Please sign in to comment.