[WIP] Day 7

This commit is contained in:
2022-12-07 19:19:46 +01:00
parent 983ebde050
commit a284052dcd
3 changed files with 108 additions and 1 deletions

23
input/day7_example.txt Normal file
View File

@@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

81
src/day7.rs Normal file
View File

@@ -0,0 +1,81 @@
use crate::day_solver::DaySolver;
use super::util;
#[derive(Debug)]
enum FileSystemEntry {
File {
name: String,
size: usize
},
Directory {
name: String,
}
}
#[derive(Debug)]
enum Command {
LS { result: Vec<FileSystemEntry> },
CD { target: String }
}
pub struct Day7 {
commands: Vec<Command>
}
impl Day7 {
pub fn create() -> Self {
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" {
let mut result = Vec::new();
if let Some(result_line) = lines_iter.peek() {
if !result_line.starts_with("$") {
if result_line.starts_with("dir") {
result.push(FileSystemEntry::Directory {
name: result_line.split_whitespace().skip(1).next().unwrap().to_string()
})
} else {
let mut result_split = result_line.split_whitespace();
result.push(FileSystemEntry::File {
size: result_split.next().unwrap().parse::<usize>().unwrap(),
name: result_split.next().unwrap().to_owned()
});
}
lines_iter.next();
}
}
commands.push(Command::LS {
result
});
} else if line.starts_with("$ cd") {
commands.push(Command::CD {
target: line.chars().skip(5).collect()
});
}
}
println!("{:?}", commands);
// Put the input into the day struct
return Day7 {
commands
}
}
}
impl DaySolver for Day7 {
fn solve_part1(&mut self) -> String {
return 0.to_string();
}
fn solve_part2(&mut self) -> String {
return 0.to_string();
}
}

View File

@@ -5,6 +5,7 @@ use crate::day3::Day3;
use crate::day4::Day4;
use crate::day5::Day5;
use crate::day6::Day6;
use crate::day7::Day7;
use crate::day_solver::DaySolver;
mod util;
@@ -15,8 +16,9 @@ mod day3;
mod day4;
mod day5;
mod day6;
mod day7;
const MAX_DAY: u8 = 5;
const MAX_DAY: u8 = 7;
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
fn main() {
@@ -86,6 +88,7 @@ fn build_day_solver(day: u8) -> Option<Box<dyn DaySolver>> {
4 => Some(Box::new(Day4::create())),
5 => Some(Box::new(Day5::create())),
6 => Some(Box::new(Day6::create())),
7 => Some(Box::new(Day7::create())),
_ => None
}
}