Skip to content

Commit

Permalink
Day 7. Part 2 plus huge refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fistons committed Dec 7, 2022
1 parent de815fb commit 1c8e079
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 43 deletions.
70 changes: 28 additions & 42 deletions aoc_7/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::collections::HashMap;
#[derive(Debug)]
enum Entry {
Cd(String),
Ls,
Dir(String),
GoUp,
File(u32, String),
}

Expand All @@ -22,68 +21,55 @@ pub fn part1(input_path: &str) -> Option<u32> {
pub fn part2(input_path: &str) -> Option<u32> {
let map = my_own_du(input_path)?;

println!("{map:?}");
let total_size = 70000000;
let needed_space = 30000000;
let unused_space = map.get("/").unwrap();
let unused_space = total_size - map.get("/").unwrap();
let space_to_clean = needed_space - unused_space;

println!("Unused space {}", unused_space);

None
map.iter()
.filter_map(|(_, y)| if *y >= space_to_clean { Some(*y) } else { None })
.min()
}

fn my_own_du(input_path: &str) -> Option<HashMap<String, u32>> {
let instructions: Vec<Entry> = std::fs::read_to_string(input_path)
.ok()?
.lines()
.map(parse_line)
.collect();

let mut current_dir = String::default();
let mut map = HashMap::<String, u32>::new();
let mut directory_path: Vec<String> = vec![];

for entry in instructions {
match entry {
Entry::Cd(change_dir) => {
if change_dir == ".." {
let total_size_precedent_dir = map.get(&current_dir).unwrap();
directory_path.pop().unwrap();
current_dir = directory_path.join("/");
//*(map.entry("toto".to_owned()).or_default()) += *total_size_precedent_dir;
*map.get_mut(&current_dir).unwrap() += *total_size_precedent_dir;
} else {
directory_path.push(change_dir.clone());
current_dir = directory_path.join("/");
map.insert(current_dir.clone(), 0);
std::fs::read_to_string(input_path)
.ok()?
.lines()
.filter_map(parse_line)
.for_each(|entry| match entry {
Entry::File(file_size, _file_name) => {
for directory in &directory_path {
*map.entry(directory.clone()).or_default() += file_size;
}
}
Entry::File(file_size, _file_name) => {
// Add the size of the file to the current directory
*map.get_mut(&current_dir).unwrap() += file_size;
Entry::Cd(change_dir) => {
let mut complete_path = directory_path.join("/");
complete_path.push_str(&change_dir);
directory_path.push(complete_path);
}
_ => (),
}
}
Entry::GoUp => {
directory_path.pop().unwrap();
}
});

Some(map)
}

fn parse_line(line: &str) -> Entry {
fn parse_line(line: &str) -> Option<Entry> {
match line {
x if x.starts_with("$ cd ..") => Some(Entry::GoUp),
x if x.starts_with("$ cd ") => {
let directory_name = x.split_whitespace().collect::<Vec<&str>>()[2];
Entry::Cd(directory_name.to_owned())
}
x if x.starts_with("$ ls") => Entry::Ls,
x if x.starts_with("dir") => {
let directory_name = x.split_whitespace().collect::<Vec<&str>>()[1];
Entry::Dir(directory_name.to_owned())
Some(Entry::Cd(directory_name.to_owned()))
}
x => {
x if x.starts_with(char::is_numeric) => {
let input = x.split_whitespace().collect::<Vec<&str>>();
Entry::File(input[0].parse().unwrap(), input[1].to_owned())
Some(Entry::File(input[0].parse().unwrap(), input[1].to_owned()))
}
_ => None,
}
}

Expand Down
2 changes: 1 addition & 1 deletion aoc_7/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use aoc_7::{part1, part2};

fn main() {
if let Some(u) = part1("input.txt") {
println!("Part 1 : {u}");
println!("Part 1: {u}");
}

if let Some(u) = part2("input.txt") {
Expand Down

0 comments on commit 1c8e079

Please sign in to comment.