[TASK] Solved Day 7

This commit is contained in:
2022-12-10 16:30:05 +01:00
parent a284052dcd
commit 8754356dbc
4 changed files with 1218 additions and 8 deletions

45
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'advent-of-code-2022-rust'",
"cargo": {
"args": [
"build",
"--bin=advent-of-code-2022-rust",
"--package=advent-of-code-2022-rust"
],
"filter": {
"name": "advent-of-code-2022-rust",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'advent-of-code-2022-rust'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=advent-of-code-2022-rust",
"--package=advent-of-code-2022-rust"
],
"filter": {
"name": "advent-of-code-2022-rust",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

1089
input/day7.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,4 @@
use crate::day_solver::DaySolver;
use super::util;
@@ -26,15 +27,15 @@ pub struct Day7 {
impl Day7 {
pub fn create() -> Self {
let lines = util::read_file("input/day7_example.txt");
// let lines = util::read_file("input/day7.txt");
// let lines = util::read_file("input/day7_example.txt");
let lines = util::read_file("input/day7.txt");
let mut lines_iter = lines.iter().peekable();
let mut commands = Vec::new();
while let Some(line) = lines_iter.next() {
if line == "$ ls" {
if line == "$ ls" {
let mut result = Vec::new();
if let Some(result_line) = lines_iter.peek() {
while let Some(result_line) = lines_iter.peek() {
if !result_line.starts_with("$") {
if result_line.starts_with("dir") {
result.push(FileSystemEntry::Directory {
@@ -48,6 +49,8 @@ impl Day7 {
});
}
lines_iter.next();
} else {
break;
}
}
commands.push(Command::LS {
@@ -60,22 +63,95 @@ impl Day7 {
}
}
println!("{:?}", commands);
// println!("{:?}", commands);
// Put the input into the day struct
return Day7 {
commands
}
}
fn calculate_dir_sizes(&self) -> Vec<(Vec<String>, usize)> {
let mut dir_stack = Vec::new();
let mut size_stack: Vec<usize> = Vec::new();
size_stack.push(0);
let mut dir_sizes: Vec<(Vec<String>, usize)> = Vec::new();
for command in &self.commands {
// In the first pass, we only count files directly under it, then in the final pass we also include children.
match command {
Command::CD { target } => {
if target == ".." {
move_up_dir(&mut dir_sizes, &mut dir_stack, &mut size_stack);
} else if target == "/" {
move_to_root(&mut dir_stack, &mut dir_sizes, &mut size_stack)
} else {
dir_stack.push(target.clone());
size_stack.push(0);
}
}
Command::LS { result } => {
let file_size: usize = result.iter()
.map(|f| if let FileSystemEntry::File { name: _, size } = f { size.to_owned() } else { 0usize })
.sum();
increment_last(&mut size_stack, file_size);
}
}
}
move_to_root(&mut dir_stack, &mut dir_sizes, &mut size_stack);
let root_size: usize = size_stack.last().unwrap().to_owned();
// dir_sizes.iter()
// .filter_map(|(dir, size)| if dir.len() == 1 { Some(size) } else { None })
// .sum::<usize>();
dir_sizes.push((Vec::new(), root_size));
println!("{:?}", dir_sizes.iter().map(|d| format!("/{}: {}", d.0.join("/"), d.1)).collect::<Vec<String>>());
dir_sizes
}
}
impl DaySolver for Day7 {
fn solve_part1(&mut self) -> String {
return 0.to_string();
let dir_sizes = self.calculate_dir_sizes();
return dir_sizes.iter()
.filter_map(|(_, size)| if size <= &100000usize { Some(size)} else { None })
.sum::<usize>().to_string();
}
fn solve_part2(&mut self) -> String {
return 0.to_string();
let dir_sizes = self.calculate_dir_sizes();
let root_size = dir_sizes.last().unwrap().1;
let min_delete_size = 30_000_000usize - (70_000_000usize - root_size);
return dir_sizes.iter().map(|d| d.1)
.filter(|s| s > &min_delete_size)
.min().unwrap().to_string();
}
}
fn move_to_root(dir_stack: &mut Vec<String>, dir_sizes: &mut Vec<(Vec<String>, usize)>, size_stack: &mut Vec<usize>) {
while !dir_stack.is_empty() {
move_up_dir(dir_sizes, dir_stack, size_stack);
}
}
fn move_up_dir(dir_sizes: &mut Vec<(Vec<String>, usize)>, dir_stack: &mut Vec<String>, size_stack: &mut Vec<usize>) {
// If we leave the directory, we should have collected all sizes of subdirs
dir_sizes.push((dir_stack.to_owned(), size_stack.last().unwrap().to_owned()));
dir_stack.pop();
// We also need to add the child size to the size of the "parent"
if let Some(child_size) = size_stack.pop() {
increment_last(size_stack, child_size);
} else {
println!("I kind of expected to always have a child size?? @ {:?}", dir_stack);
}
}
fn increment_last(size_stack: &mut Vec<usize>, size: usize) {
if let Some(last) = size_stack.last_mut() {
*last += size;
}
}

View File

@@ -112,7 +112,7 @@ fn solve(day: u8, silent: bool) -> AocBenchResult {
let (part2, pt2_time) = bench(|| s.solve_part2());
part2_time = pt2_time;
if !silent {
println!("Day {} Part 1: {}", day, part2);
println!("Day {} Part 2: {}", day, part2);
}
},
None => panic!("This day is not yet implemented")