This commit is contained in:
max.nuding 2022-12-07 09:17:05 +01:00
parent 3cca0097ed
commit cb204b8d62
Failed to extract signature
4 changed files with 1068 additions and 7 deletions

7
Cargo.lock generated
View File

@ -25,6 +25,7 @@ name = "aoc-2022"
version = "0.1.0"
dependencies = [
"chrono",
"indextree",
"itertools",
"regex",
"tuple-map",
@ -159,6 +160,12 @@ dependencies = [
"cxx-build",
]
[[package]]
name = "indextree"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497f036ac2fae75c34224648a77802e5dd4e9cfb56f4713ab6b12b7160a0523b"
[[package]]
name = "itertools"
version = "0.10.5"

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
chrono = "0.4.23"
indextree = "4.5.0"
itertools = "0.10.5"
regex = "1.7.0"
tuple-map = "0.4.0"

File diff suppressed because it is too large Load Diff

View File

@ -1,18 +1,68 @@
use crate::read;
use itertools::Itertools;
use tuple_map::*;
use indextree::Arena;
#[derive(Debug)]
struct Directory {
pub size: usize,
pub name: String
}
pub fn run() {
let input = read("04");
let lines = input.lines();
let lines = input.split("\n\n");
let arena = &mut Arena::new();
let mut current_directory = arena.new_node(Directory {
size: 0,
name: "/".into(),
});
let root = current_directory;
for line in read("07").lines().skip(1) {
if line.starts_with("$ cd") {
current_directory = match line.split_once("cd ").unwrap().1 {
dirname if ".." == dirname => current_directory.ancestors(arena).nth(1).unwrap(),
dirname => current_directory
.children(arena)
.find(|c|arena.get(*c).unwrap().get().name == dirname)
.unwrap_or_else(||{
let nc = arena.new_node(Directory {
size: 0,
name: dirname.into(),
});
current_directory.append(nc, arena);
nc
})
};
} else if line.chars().next().unwrap().is_numeric() {
let filesize: usize = line.split_once(" ").unwrap().0.parse().unwrap();
arena.get_mut(current_directory).unwrap().get_mut().size += filesize;
}
}
let total = root.descendants(arena).map(|desc|{
desc
.descendants(arena)
.map(|d|arena.get(d).unwrap().get().size)
.sum::<usize>()
}).collect::<Vec<_>>();
#[cfg(feature = "part1")]
{
println!("Day 7, Part 01: {}", "TODO");
println!("Day 7, Part 01: {}", total.clone().iter()
.filter(|s| *s <= &100000)
.sum::<usize>());
}
#[cfg(feature = "part2")]
{
println!("Day 7, Part 02: {}", "TODO");
let total_used = root.descendants(arena)
.map(|d|arena.get(d).unwrap().get().size)
.sum::<usize>();
let free_needed = total_used - 40000000;
println!("Day 7, Part 02: {}", total.iter()
.filter(|s|*s >= &free_needed)
.sorted()
.next()
.unwrap());
}
}