feat: start with day 7

This commit is contained in:
Lennard Brinkhaus 2022-12-07 15:14:49 +01:00
parent 9ee13cc480
commit f27b6274ce
Signed by: lennard.brinkhaus
GPG Key ID: 286421EC53998B22
6 changed files with 1201 additions and 2 deletions

View File

@ -52,7 +52,6 @@ pub fn convert_char_to_prio(c: char) -> u8 {
mod test {
const EXAMPLE01: &'static str = include_str!("input/day03/example01");
#[test]
fn test_first() {
assert_eq!(super::solve_task01(EXAMPLE01), 157);

View File

@ -99,7 +99,9 @@ fn move_in_port_currently(port: &mut Vec<LinkedList<String>>, from: &str, to: &s
}
fn move_in_port_once(port: &mut Vec<LinkedList<String>>, from: &str, to: &str) {
let tmp: String = port.get_mut((from.parse::<usize>().unwrap()) -1).unwrap().pop_back().unwrap();
let tmp: String = port.get_mut(
(from.parse::<usize>().unwrap()) -1).unwrap().pop_back().unwrap();
port.get_mut((to.parse::<usize>().unwrap()) -1).unwrap().push_back(tmp);
}

144
src/day07.rs Normal file
View File

@ -0,0 +1,144 @@
use crate::utils;
#[derive(Debug,Clone)]
struct File {
name: String,
size: i32,
}
#[derive(Debug,Clone)]
struct Folder{
root: Option<Box<Folder>>,
name: String,
folder: Vec<Folder>,
files: Vec<File>,
}
impl Folder {
fn add_folder(&mut self, folder: Folder) {
self.folder.push(folder);
}
fn add_file(&mut self, file: File) {
self.files.push(file);
}
fn get_size(self) -> i32 {
let file_sum: i32 = self.files.into_iter().map(|file| file.size).sum();
let folder_sum: i32 = self.folder.into_iter().map(|folder| folder.get_size()).sum();
return file_sum + folder_sum;
}
fn flat_folder(self) -> Vec<Folder> {
let mut sub_folder = vec![];
for folder in self.folder {
sub_folder.push(folder);
sub_folder.extend(folder.flat_folder());
}
return sub_folder;
}
}
pub fn execute_task01(content: &str) {
let line = solve_task01(content);
println!("Day07 - Task01 - Sum of total Size: {}", line);
}
pub fn execute_task02(content: &str) {
let line = solve_task02(content);
println!("Day07 - Task02 - Last Item Str Line: {}", line);
}
fn solve_task01(content: &str) -> i32 {
let root: &Folder = parse_tree(content);
let full_size: i32 = root.flat_folder()
.into_iter()
.map(|folder| folder.get_size())
.filter(|size| size.clone() <= 100000)
.sum();
return full_size;
}
fn solve_task02(content: &str) -> i32 {
return 0;
}
fn parse_tree(content: &str) -> &Folder {
let vector = utils::convert_to_string_slice(&content);
let root: &Folder = &Folder{
root: None,
name: "".parse().unwrap(),
folder: vec![],
files: vec![],
};
let mut current: &Folder = &root;
for line in vector.into_iter() {
if line.starts_with("$") {
if line.starts_with("$ cd") {
let select = line.split(" ").collect::<Vec<&str>>()[3..].join("");
if select == "/" {
current = &root;
} else if select == ".." {
current = &current.root.unwrap();
} else {
for child_folder in current.folder {
if child_folder.name == select {
current = &child_folder;
break;
}
}
}
} else if line.starts_with("$ ls") {
continue
} else {
let parts: Vec<&str> = line.split(" ").collect();
let part1 = *parts.get(0).unwrap();
let part2 = *parts.get(1).unwrap();
if part1 == "dir" {
current.add_folder(Folder {
root: Some(Box::new(*current)),
name: part2.parse().unwrap(),
folder: vec![],
files: vec![],
});
} else {
current.add_file(File{
name: part2.parse().unwrap(),
size: part1.parse::<i32>().unwrap(),
});
}
}
}
}
return root;
}
#[cfg(test)]
mod test {
const EXAMPLE01: &'static str = include_str!("input/day07/example01");
#[test]
fn test_first() {
assert_eq!(super::solve_task01(EXAMPLE01), 95437);
}
#[test]
fn test_second() {
}
}

23
src/input/day07/example01 Normal file
View File

@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

1030
src/input/day07/input01 Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@ mod day03;
mod day04;
mod day05;
mod day06;
mod day07;
const CONTENT01: &'static str = include_str!("input/day01/input01");
const CONTENT02: &'static str = include_str!("input/day02/input01");