diff --git a/07/main.rs b/07/main.rs index 840f8eb..13137af 100644 --- a/07/main.rs +++ b/07/main.rs @@ -1,17 +1,10 @@ #![allow(dead_code)] -use std::collections::HashMap; use std::fs::File; use std::io; use std::io::BufRead; use std::path::Path; use std::time::Instant; -#[derive(Debug, Clone)] -struct Folder { - folders: HashMap, - size: u32, -} - fn main() { let now = Instant::now(); part_one(); @@ -71,4 +64,40 @@ fn part_one() { } } -fn part_two() {} +fn part_two() { + if let Ok(lines) = read_lines("./inputs/input") { + let mut stack: Vec = vec![0]; + let space_to_free = 30000000 - (70000000 - 49192532); // size of all files, precomputed + let mut to_delete = 70000000; + for line in lines { + if let Ok(command) = line { + match command.as_str() { + "$ cd /" => continue, // skip start + "$ ls" => continue, // we can assume this if we're not moving between directories + s if s.starts_with("dir ") => continue, // we'll get there when we `cd` + "$ cd .." => { + let f = stack.pop().unwrap(); + if f > space_to_free && f < to_delete { + to_delete = f + } + let mut p = stack.pop().unwrap(); + p += f; + stack.push(p); + } + s if s.starts_with("$ cd ") => { + stack.push(0); + } + s => { + // otherwise it must be a file + let mut f = stack.pop().unwrap(); + let mut sp = s.split(" "); + let size = sp.next().unwrap().parse::().unwrap(); + f += size; + stack.push(f); + } + }; + } + } + println!("{:?}", to_delete); + } +}