Failed at day 7 for now, mut commiting anyway

This commit is contained in:
Bizcochito 2022-12-07 10:52:02 +01:00
parent 5331fa6bf3
commit cd1aee3277
4 changed files with 99 additions and 1 deletions

4
Cargo.lock generated
View File

@ -26,6 +26,10 @@ version = "0.1.0"
name = "day06"
version = "0.1.0"
[[package]]
name = "day07"
version = "0.1.0"
[[package]]
name = "tiesto"
version = "0.1.0"

View File

@ -8,7 +8,7 @@ members = [
"day04",
"day05",
"day06",
# "day07",
"day07",
# "day08",
# "day09",
# "day10",

8
day07/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day07"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

86
day07/src/main.rs Normal file
View File

@ -0,0 +1,86 @@
use std::fs;
use std::str::FromStr;
use std::collections::HashMap;
fn main() {
const FILE_PATH: &str = "input";
println!("Hi this is the sixth day of AOC2022, first we will read the file {}", FILE_PATH);
let contents = fs::read_to_string(FILE_PATH)
.expect("Should have been able to read the file");
}
#[derive(Debug)]
enum FileSys<'a>{
File(u32),
Dir(HashMap<&'a str, FileSys<'a>>)
}
fn traverse<'a>(root: &'a mut HashMap<&'a str, FileSys<'a>>, ptr: &'a Vec<&'a str>) -> HashMap<&'a str, FileSys<'a>>{
let mut out = root;
for dir in *ptr{
out = match out.get_mut(dir).expect("u messed up the dirs") {
FileSys::Dir(x) => x,
_ => panic!("u messed up the dirs, tis a file")
}
};
*out
}
fn parse(s: &str) -> HashMap<&str, FileSys>{
let mut root = HashMap::new();
let mut s = s.lines();
s.next();
let mut ptr: Vec<&str> = vec![];
for line in s {
let mut line = line.split(" ");
match line.next().expect("lol") {
"$" => if line.next().expect("must have a command") == "cd"{
match line.next().expect("must have a name") {
".." => ptr.pop(),
name => {ptr.push(name); None},
};
}else{continue;},
"dir" =>{
let root = traverse(&mut root, &ptr);
root.insert(line.next().expect("must have a name"), FileSys::Dir(HashMap::new()));
},
size =>{
let root = traverse(&mut root, &ptr);
root.insert(line.next().expect("must have a name"), FileSys::File(u32::from_str(size).expect("NaN")));
},
}
}
root
}
#[cfg(test)]
mod test {
const INPUT: &str = r#"$ 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"#;
use super::*;
#[test]
fn coms1_1() {
dbg!(parse(INPUT));
}
}