Compare commits
3 Commits
453590c541
...
42c911c9a2
Author | SHA1 | Date | |
---|---|---|---|
42c911c9a2 | |||
0b9a1061a0 | |||
340b207f81 |
47
src/day07.rs
47
src/day07.rs
@ -23,14 +23,6 @@ impl DirectoryEntry {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_dir(&self) -> Option<&String> {
|
||||
if let Self::Dir(v) = self {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
@ -58,16 +50,6 @@ enum CliOperation {
|
||||
Ls(Vec<DirectoryEntry>),
|
||||
}
|
||||
|
||||
impl CliOperation {
|
||||
fn as_ls(&self) -> Option<&Vec<DirectoryEntry>> {
|
||||
if let Self::Ls(v) = self {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[aoc_generator(day7)]
|
||||
fn input_generator(input: &str) -> Vec<CliOperation> {
|
||||
let mut lines = input.lines().peekable();
|
||||
@ -95,19 +77,7 @@ fn compute_dir_sizes(input: &[CliOperation]) -> Tree<(usize, usize)> {
|
||||
}
|
||||
let mut tree = Tree::new();
|
||||
let mut current_node = tree
|
||||
.insert(
|
||||
Node::new((
|
||||
input[1]
|
||||
.as_ls()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(DirectoryEntry::as_file)
|
||||
.map(|file| file.size)
|
||||
.sum::<usize>(),
|
||||
0,
|
||||
)),
|
||||
InsertBehavior::AsRoot,
|
||||
)
|
||||
.insert(Node::new((0, 0)), InsertBehavior::AsRoot)
|
||||
.unwrap();
|
||||
for op in input {
|
||||
match op {
|
||||
@ -151,16 +121,11 @@ fn compute_dir_sizes(input: &[CliOperation]) -> Tree<(usize, usize)> {
|
||||
#[aoc(day7, part1)]
|
||||
fn solve_part1(input: &[CliOperation]) -> usize {
|
||||
let tree = compute_dir_sizes(input);
|
||||
let mut sum = 0;
|
||||
for node in tree
|
||||
.traverse_post_order(tree.root_node_id().unwrap())
|
||||
tree.traverse_post_order(tree.root_node_id().unwrap())
|
||||
.unwrap()
|
||||
{
|
||||
if node.data().1 <= 100000 {
|
||||
sum += node.data().1;
|
||||
}
|
||||
}
|
||||
sum
|
||||
.map(|node| node.data().1)
|
||||
.filter(|&size| size <= 100_000)
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[aoc(day7, part2)]
|
||||
@ -170,8 +135,8 @@ fn solve_part2(input: &[CliOperation]) -> usize {
|
||||
let necessary_to_free = 30_000_000 - free_space;
|
||||
tree.traverse_post_order(tree.root_node_id().unwrap())
|
||||
.unwrap()
|
||||
.filter(|node| node.data().1 >= necessary_to_free)
|
||||
.map(|node| node.data().1)
|
||||
.filter(|&size| size >= necessary_to_free)
|
||||
.min()
|
||||
.unwrap()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user