Initial work on 2022/day07-part1
This commit is contained in:
parent
c737e0ed67
commit
02afcc631b
8
2022/day07-part1/Cargo.toml
Normal file
8
2022/day07-part1/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day07-part1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
65
2022/day07-part1/src/main.rs
Normal file
65
2022/day07-part1/src/main.rs
Normal file
@ -0,0 +1,65 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
// Custom data structure
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
enum NodeType {
|
||||
File,
|
||||
Directory,
|
||||
}
|
||||
|
||||
use NodeType::*;
|
||||
|
||||
struct Node {
|
||||
node_type: NodeType,
|
||||
name: String,
|
||||
size: usize,
|
||||
children: Vec<Rc<RefCell<Node>>>,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
fn new(node_type: NodeType, name: &str, size: usize) -> Rc<RefCell<Node>> {
|
||||
Rc::new(RefCell::new(Node {
|
||||
node_type,
|
||||
name: String::from(name),
|
||||
size,
|
||||
children: Vec::new(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Use command line arguments to specify the input filename.
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
panic!("Usage: ./main <input-file>\nNo input file provided. Exiting.");
|
||||
}
|
||||
|
||||
// Next, read the contents of the input file into a string for easier processing.
|
||||
let input = std::fs::read_to_string(&args[1]).expect("Error opening file");
|
||||
// Line-by-line processing is easiest.
|
||||
let input = input.lines();
|
||||
|
||||
// --- TASK BEGIN ---
|
||||
let mut result = 0;
|
||||
|
||||
// Set up our filesystem, starting with the root directory as the root node.
|
||||
let root = Node::new(Directory, "/", 0);
|
||||
|
||||
// Also set up a
|
||||
let current = root;
|
||||
|
||||
// Read line by line
|
||||
for line in input {
|
||||
// Split any incoming line by spaces, makes our life easier down the line.
|
||||
let line = line.split(' ').collect::<Vec<_>>();
|
||||
|
||||
// First of all, differentiate between command and list item.
|
||||
if line[0] == "$" {
|
||||
|
||||
} else {
|
||||
println!("item");
|
||||
}
|
||||
}
|
||||
|
||||
println!("Result: {}", result);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user