-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |