From cd1aee3277c705b21bd59a562376acafe15e04f4 Mon Sep 17 00:00:00 2001 From: Bizcochito Date: Wed, 7 Dec 2022 10:52:02 +0100 Subject: [PATCH] Failed at day 7 for now, mut commiting anyway --- Cargo.lock | 4 +++ Cargo.toml | 2 +- day07/Cargo.toml | 8 +++++ day07/src/main.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 day07/Cargo.toml create mode 100644 day07/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 7b3d0a7..e5587ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 9e9a53c..bfb26ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ members = [ "day04", "day05", "day06", -# "day07", + "day07", # "day08", # "day09", # "day10", diff --git a/day07/Cargo.toml b/day07/Cargo.toml new file mode 100644 index 0000000..682e4ad --- /dev/null +++ b/day07/Cargo.toml @@ -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] diff --git a/day07/src/main.rs b/day07/src/main.rs new file mode 100644 index 0000000..d223ad0 --- /dev/null +++ b/day07/src/main.rs @@ -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)); + } +} \ No newline at end of file