Compare commits
No commits in common. "a74b1f4bc6008ad21b70f73488b21e2a64158af7" and "23418f98be8e2547c1b9935517d03fcc4b9341a2" have entirely different histories.
a74b1f4bc6
...
23418f98be
2 changed files with 52 additions and 53 deletions
|
@ -1,62 +1,76 @@
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
use std::{collections::HashMap, io::stdin, path::PathBuf};
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
hash::Hash,
|
||||||
|
io::{stdin, Read},
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
use aoc::PathExt;
|
use aoc::*;
|
||||||
|
|
||||||
fn find_dir_to_delete(dirs: &HashMap<PathBuf, usize>) -> usize {
|
|
||||||
const TOTAL_SPACE: usize = 70_000_000;
|
|
||||||
const REQUIRED_SPACE: usize = 30_000_000;
|
|
||||||
|
|
||||||
let unused = TOTAL_SPACE - dirs[&*PathBuf::from("/")];
|
|
||||||
let to_free = REQUIRED_SPACE - unused;
|
|
||||||
|
|
||||||
let mut sizes: Vec<_> = dirs.values().copied().collect();
|
|
||||||
sizes.sort_unstable();
|
|
||||||
match sizes.binary_search(&to_free) {
|
|
||||||
Ok(i) | Err(i) => sizes[i],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let mut data = String::new();
|
||||||
|
stdin().read_to_string(&mut data).unwrap();
|
||||||
|
|
||||||
let mut cwd = PathBuf::new();
|
let mut cwd = PathBuf::new();
|
||||||
cwd.push("/");
|
cwd.push("/");
|
||||||
let mut dirs: HashMap<PathBuf, usize> = HashMap::new();
|
let mut dirs: HashMap<PathBuf, usize> = HashMap::new();
|
||||||
|
|
||||||
for line in stdin().lines() {
|
for line in data.lines() {
|
||||||
let line = line.unwrap();
|
|
||||||
|
|
||||||
if let Some(command) = line.strip_prefix("$ ") {
|
if let Some(command) = line.strip_prefix("$ ") {
|
||||||
let mut args = command.split_whitespace();
|
let mut args = command.split_whitespace();
|
||||||
|
let command = args.next().unwrap();
|
||||||
if args.next().unwrap() != "cd" {
|
match command {
|
||||||
continue;
|
"ls" => {
|
||||||
}
|
dirs.remove(&cwd);
|
||||||
|
|
||||||
match args.next().unwrap() {
|
|
||||||
".." => assert!(cwd.pop()),
|
|
||||||
path => {
|
|
||||||
cwd.push(path);
|
|
||||||
assert!(!dirs.contains_key(&cwd));
|
|
||||||
}
|
}
|
||||||
|
"cd" => {
|
||||||
|
let dir = args.next().unwrap();
|
||||||
|
match dir {
|
||||||
|
".." => assert!(cwd.pop()),
|
||||||
|
path => cwd.push(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => panic!("unknown command {command}"),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let size = line.split_whitespace().next().unwrap();
|
let (size, _name) = line.split_once(' ').unwrap();
|
||||||
if size == "dir" {
|
if size == "dir" {
|
||||||
continue;
|
// skip
|
||||||
|
} else {
|
||||||
|
let size: usize = size.parse().unwrap();
|
||||||
|
*dirs.entry(cwd.clone()).or_default() += size;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let size: usize = size.parse().unwrap();
|
let mut dirs_recursive: HashMap<&Path, usize> = HashMap::new();
|
||||||
for dir in cwd.parents() {
|
for (dir, size) in &dirs {
|
||||||
*dirs.entry(dir.to_path_buf()).or_default() += size;
|
for parent in std::iter::successors(Some(&**dir), |dir| dir.parent()) {
|
||||||
}
|
*dirs_recursive.entry(parent).or_default() += size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
dirs.values().filter(|&&s| s <= 100_000).sum::<usize>()
|
dirs_recursive
|
||||||
|
.values()
|
||||||
|
.filter(|&&s| s <= 100_000)
|
||||||
|
.sum::<usize>()
|
||||||
);
|
);
|
||||||
|
|
||||||
println!("{}", find_dir_to_delete(&dirs));
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
mod section_range;
|
mod section_range;
|
||||||
|
|
||||||
use std::{mem, path::Path};
|
use std::mem;
|
||||||
|
|
||||||
pub use section_range::{EmptyRange, InvalidSectionString, SectionRange};
|
pub use section_range::{EmptyRange, InvalidSectionString, SectionRange};
|
||||||
|
|
||||||
|
@ -61,18 +61,3 @@ impl<T> Iterator for UpToTwoIter<T> {
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait PathExt {
|
|
||||||
type Iter<'a>: Iterator<Item = &'a Path>
|
|
||||||
where
|
|
||||||
Self: 'a;
|
|
||||||
fn parents(&self) -> Self::Iter<'_>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PathExt for Path {
|
|
||||||
type Iter<'a> = std::iter::Successors<&'a Path, for<'p> fn(&&'p Path) -> Option<&'p Path>>;
|
|
||||||
|
|
||||||
fn parents(&self) -> Self::Iter<'_> {
|
|
||||||
std::iter::successors(Some(self), |dir| dir.parent())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue