2022 day7/rust: add solution

This commit is contained in:
Xiretza 2022-12-07 06:29:48 +01:00
parent 28718710cf
commit 23418f98be
4 changed files with 93 additions and 0 deletions

View 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" }

View 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
View file

@ -510,6 +510,13 @@ dependencies = [
"aoc",
]
[[package]]
name = "rust_2022_07"
version = "0.1.0"
dependencies = [
"aoc",
]
[[package]]
name = "ryu"
version = "1.0.11"

View file

@ -16,4 +16,5 @@ members = [
"2022/day4/rust",
"2022/day5/rust",
"2022/day6/rust",
"2022/day7/rust",
]