Compare commits
No commits in common. "23418f98be8e2547c1b9935517d03fcc4b9341a2" and "59c1d54801a86fc6b485ff1871f6ea0b998dd7d9" have entirely different histories.
23418f98be
...
59c1d54801
6 changed files with 0 additions and 1125 deletions
|
@ -1,2 +0,0 @@
|
||||||
1477771
|
|
||||||
3579501
|
|
1030
2022/data/day7.input
1030
2022/data/day7.input
File diff suppressed because it is too large
Load diff
|
@ -1,9 +0,0 @@
|
||||||
[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" }
|
|
|
@ -1,76 +0,0 @@
|
||||||
#![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,13 +510,6 @@ dependencies = [
|
||||||
"aoc",
|
"aoc",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rust_2022_07"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"aoc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.11"
|
version = "1.0.11"
|
||||||
|
|
|
@ -16,5 +16,4 @@ members = [
|
||||||
"2022/day4/rust",
|
"2022/day4/rust",
|
||||||
"2022/day5/rust",
|
"2022/day5/rust",
|
||||||
"2022/day6/rust",
|
"2022/day6/rust",
|
||||||
"2022/day7/rust",
|
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue