dau 7 done
This commit is contained in:
parent
cd1aee3277
commit
deaa7d6db8
File diff suppressed because it is too large
Load Diff
|
@ -4,53 +4,44 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
const FILE_PATH: &str = "input";
|
const FILE_PATH: &str = "input";
|
||||||
println!("Hi this is the sixth day of AOC2022, first we will read the file {}", FILE_PATH);
|
println!("Hi this is the seventh day of AOC2022, first we will read the file {}", FILE_PATH);
|
||||||
let contents = fs::read_to_string(FILE_PATH)
|
let contents = fs::read_to_string(FILE_PATH)
|
||||||
.expect("Should have been able to read the file");
|
.expect("Should have been able to read the file");
|
||||||
|
|
||||||
}
|
println!("The sum of all dirs less than 100000 is {}", parse(&contents).values().filter(|x| **x<100000).sum::<u32>());
|
||||||
#[derive(Debug)]
|
println!("The smallest directory to erase to make the update is {}", get_smaller_dir_for_update(&contents));
|
||||||
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>>{
|
fn parse(s: &str) -> HashMap<usize, u32>{
|
||||||
let mut out = root;
|
let mut sizes = HashMap::new();
|
||||||
for dir in *ptr{
|
let mut ptr: Vec<usize> = vec![];
|
||||||
out = match out.get_mut(dir).expect("u messed up the dirs") {
|
let mut linode: usize = 0;
|
||||||
FileSys::Dir(x) => x,
|
for line in s.lines() {
|
||||||
_ => 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(" ");
|
let mut line = line.split(" ");
|
||||||
match line.next().expect("lol") {
|
match line.next().expect("lol") {
|
||||||
"$" => if line.next().expect("must have a command") == "cd"{
|
"$" => if line.next().expect("must have a command") == "cd"{
|
||||||
match line.next().expect("must have a name") {
|
match line.next().expect("must have a name") {
|
||||||
".." => ptr.pop(),
|
".." => {ptr.pop(); ()},
|
||||||
name => {ptr.push(name); None},
|
_ => {ptr.push(linode); sizes.insert(linode, 0); linode+=1;},
|
||||||
};
|
};
|
||||||
}else{continue;},
|
}else{continue;},
|
||||||
"dir" =>{
|
"dir" =>{
|
||||||
let root = traverse(&mut root, &ptr);
|
continue
|
||||||
root.insert(line.next().expect("must have a name"), FileSys::Dir(HashMap::new()));
|
|
||||||
},
|
},
|
||||||
size =>{
|
size =>{
|
||||||
let root = traverse(&mut root, &ptr);
|
ptr.iter().for_each(|d| *sizes.get_mut(d).expect("dir must exist")+= u32::from_str(size).expect("NaN"));
|
||||||
root.insert(line.next().expect("must have a name"), FileSys::File(u32::from_str(size).expect("NaN")));
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
root
|
sizes
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_smaller_dir_for_update(input: &str) -> u32 {
|
||||||
|
let sol = parse(input);
|
||||||
|
let target = 30000000 - (70000000 - sol.get(&0).expect("Root must exist"));
|
||||||
|
let mut sol: Vec<_> = sol.values().collect();
|
||||||
|
sol.sort();
|
||||||
|
**sol.iter().filter(|x| ***x > target).next().expect("If there is not enough dir that mean the update is already posible dingus")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -80,7 +71,11 @@ $ ls
|
||||||
7214296 k"#;
|
7214296 k"#;
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn coms1_1() {
|
fn update1() {
|
||||||
dbg!(parse(INPUT));
|
assert_eq!(parse(INPUT).values().filter(|x| **x<100000).sum::<u32>(), 95437);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn update2() {
|
||||||
|
assert_eq!(get_smaller_dir_for_update(INPUT), 24933642);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue