day 07
This commit is contained in:
parent
737c255cc6
commit
2cc145606c
|
@ -0,0 +1,12 @@
|
|||
# ---> Rust
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "aoc_07"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,68 @@
|
|||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() {
|
||||
let file_path = String::from("input");
|
||||
let mut selected = Vec::<String>::new();
|
||||
let mut folders = HashMap::<String, u32>::new();
|
||||
|
||||
println!("In file {}", file_path);
|
||||
|
||||
if let Ok(lines) = read_lines(file_path) {
|
||||
// Consumes the iterator, returns an (Optional) String
|
||||
for line in lines {
|
||||
if let Ok(ip) = line {
|
||||
match &ip[..3] {
|
||||
"dir" => {
|
||||
let dir = String::from(ip.split(" ").nth(1).unwrap());
|
||||
folders.insert(String::from(selected.last().unwrap()) + "/" + &dir, 0);
|
||||
},
|
||||
"$ c" => {
|
||||
let dir = String::from(ip.split(" ").nth(2).unwrap());
|
||||
if dir == ".." {
|
||||
selected.pop();
|
||||
}
|
||||
else {
|
||||
if selected.len() > 0 {
|
||||
selected.push(String::from(selected.last().unwrap()) + "/" + &dir);
|
||||
}
|
||||
else {
|
||||
selected.push(String::from(""));
|
||||
}
|
||||
}
|
||||
},
|
||||
"$ l" => (),
|
||||
_ => {
|
||||
for dir in &selected {
|
||||
let file_size = String::from(ip.split(" ").nth(0).unwrap()).parse::<u32>().unwrap();
|
||||
*folders.entry(String::from(dir)).or_insert(0) += file_size;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//println!("{}", folders[""]);
|
||||
let minimum = 30000000 - (70000000 - folders[""]);
|
||||
let mut result = 0;
|
||||
let mut result2 = 70000000;
|
||||
for (dir, size) in folders {
|
||||
println!("{} {}", dir, size);
|
||||
if size < 100000 {
|
||||
result += size;
|
||||
}
|
||||
if size < result2 && size > minimum {
|
||||
result2 = size;
|
||||
}
|
||||
}
|
||||
println!("exercise 1: {} exercise 2: {}", result, result2);
|
||||
}
|
||||
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
Loading…
Reference in New Issue