Clean up handling of root node

This commit is contained in:
pjht 2022-12-07 13:22:59 -06:00
parent 453590c541
commit 340b207f81

View File

@ -23,14 +23,6 @@ impl DirectoryEntry {
None None
} }
} }
fn as_dir(&self) -> Option<&String> {
if let Self::Dir(v) = self {
Some(v)
} else {
None
}
}
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@ -58,16 +50,6 @@ enum CliOperation {
Ls(Vec<DirectoryEntry>), 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)] #[aoc_generator(day7)]
fn input_generator(input: &str) -> Vec<CliOperation> { fn input_generator(input: &str) -> Vec<CliOperation> {
let mut lines = input.lines().peekable(); 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 tree = Tree::new();
let mut current_node = tree let mut current_node = tree
.insert( .insert(Node::new((0, 0)), InsertBehavior::AsRoot)
Node::new((
input[1]
.as_ls()
.unwrap()
.iter()
.filter_map(DirectoryEntry::as_file)
.map(|file| file.size)
.sum::<usize>(),
0,
)),
InsertBehavior::AsRoot,
)
.unwrap(); .unwrap();
for op in input { for op in input {
match op { match op {