Compare commits
2 commits
59c1d54801
...
23418f98be
Author | SHA1 | Date | |
---|---|---|---|
23418f98be | |||
28718710cf |
6 changed files with 1125 additions and 0 deletions
2
2022/data/day7.expected
Normal file
2
2022/data/day7.expected
Normal file
|
@ -0,0 +1,2 @@
|
|||
1477771
|
||||
3579501
|
1030
2022/data/day7.input
Normal file
1030
2022/data/day7.input
Normal file
File diff suppressed because it is too large
Load diff
9
2022/day7/rust/Cargo.toml
Normal file
9
2022/day7/rust/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "rust_2022_07"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
aoc = { path = "../../../common/rust" }
|
76
2022/day7/rust/src/main.rs
Normal file
76
2022/day7/rust/src/main.rs
Normal file
|
@ -0,0 +1,76 @@
|
|||
#![warn(clippy::pedantic)]
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
hash::Hash,
|
||||
io::{stdin, Read},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use aoc::*;
|
||||
|
||||
fn main() {
|
||||
let mut data = String::new();
|
||||
stdin().read_to_string(&mut data).unwrap();
|
||||
|
||||
let mut cwd = PathBuf::new();
|
||||
cwd.push("/");
|
||||
let mut dirs: HashMap<PathBuf, usize> = HashMap::new();
|
||||
|
||||
for line in data.lines() {
|
||||
if let Some(command) = line.strip_prefix("$ ") {
|
||||
let mut args = command.split_whitespace();
|
||||
let command = args.next().unwrap();
|
||||
match command {
|
||||
"ls" => {
|
||||
dirs.remove(&cwd);
|
||||
}
|
||||
"cd" => {
|
||||
let dir = args.next().unwrap();
|
||||
match dir {
|
||||
".." => assert!(cwd.pop()),
|
||||
path => cwd.push(path),
|
||||
}
|
||||
}
|
||||
_ => panic!("unknown command {command}"),
|
||||
}
|
||||
} else {
|
||||
let (size, _name) = line.split_once(' ').unwrap();
|
||||
if size == "dir" {
|
||||
// skip
|
||||
} else {
|
||||
let size: usize = size.parse().unwrap();
|
||||
*dirs.entry(cwd.clone()).or_default() += size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut dirs_recursive: HashMap<&Path, usize> = HashMap::new();
|
||||
for (dir, size) in &dirs {
|
||||
for parent in std::iter::successors(Some(&**dir), |dir| dir.parent()) {
|
||||
*dirs_recursive.entry(parent).or_default() += size;
|
||||
}
|
||||
}
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
dirs_recursive
|
||||
.values()
|
||||
.filter(|&&s| s <= 100_000)
|
||||
.sum::<usize>()
|
||||
);
|
||||
|
||||
const TOTAL_SPACE: usize = 70_000_000;
|
||||
const REQUIRED_SPACE: usize = 30_000_000;
|
||||
|
||||
let unused = TOTAL_SPACE - dirs_recursive[&*PathBuf::from("/")];
|
||||
let to_free = REQUIRED_SPACE - unused;
|
||||
let mut sizes: Vec<_> = dirs_recursive.values().copied().collect();
|
||||
sizes.sort_unstable();
|
||||
let size = match sizes.binary_search(&to_free) {
|
||||
Ok(i) => sizes[i],
|
||||
Err(larger) => sizes[larger],
|
||||
};
|
||||
|
||||
println!("{}", size);
|
||||
}
|
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -510,6 +510,13 @@ dependencies = [
|
|||
"aoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_2022_07"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.11"
|
||||
|
|
|
@ -16,4 +16,5 @@ members = [
|
|||
"2022/day4/rust",
|
||||
"2022/day5/rust",
|
||||
"2022/day6/rust",
|
||||
"2022/day7/rust",
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue