Compare commits
No commits in common. "607a48eb3a2fd158aebf3b0c5382acf645a4e283" and "4c11c4df3e1bba290d24b5f85a9e8582490af1c5" have entirely different histories.
607a48eb3a
...
4c11c4df3e
|
@ -14,6 +14,10 @@ impl Range {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn calc_size(&self) -> u16 {
|
||||||
|
self.start.abs_diff(self.end)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn any_overlap(&self, other: &Self) -> bool {
|
pub fn any_overlap(&self, other: &Self) -> bool {
|
||||||
self.start <= other.end && self.end >= other.start
|
self.start <= other.end && self.end >= other.start
|
||||||
}
|
}
|
||||||
|
@ -27,6 +31,14 @@ impl Range {
|
||||||
pub fn complete_overlap(&self, other: &Self) -> bool {
|
pub fn complete_overlap(&self, other: &Self) -> bool {
|
||||||
self.calc_overlap(other) == *self || self.calc_overlap(other) == *other
|
self.calc_overlap(other) == *self || self.calc_overlap(other) == *other
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn start(&self) -> u16 {
|
||||||
|
self.start
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn end(&self) -> u16 {
|
||||||
|
self.end
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static PARSE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\d+)-(\d+),(\d+)-(\d+)").unwrap());
|
static PARSE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\d+)-(\d+),(\d+)-(\d+)").unwrap());
|
||||||
|
|
21
src/day07/fileTree.rs
Normal file
21
src/day07/fileTree.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
use std::{rc::{Weak, Rc}, cell::RefCell};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Dir {
|
||||||
|
parent: Option<Weak<RefCell<Dir>>>,
|
||||||
|
name: String,
|
||||||
|
children: Vec<Rc<RefCell<FileOrDir>>>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct File{
|
||||||
|
dir: Weak<RefCell<Dir>>,
|
||||||
|
size: usize,
|
||||||
|
name: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum FileOrDir{
|
||||||
|
File(File),
|
||||||
|
Dir(Dir),
|
||||||
|
}
|
|
@ -1,87 +0,0 @@
|
||||||
#![allow(unused)]
|
|
||||||
use std::{
|
|
||||||
cell::RefCell,
|
|
||||||
ops::Deref,
|
|
||||||
rc::{Rc, Weak},
|
|
||||||
};
|
|
||||||
|
|
||||||
// #[derive(Debug)]
|
|
||||||
// pub struct NodeRef(Rc<RefCell<Node>>);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct FileRef(Rc<RefCell<File>>);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DirRef(Rc<RefCell<Dir>>);
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct WeakDirRef(Weak<RefCell<Dir>>);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Node {
|
|
||||||
Dir(DirRef),
|
|
||||||
File(FileRef),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct File {
|
|
||||||
parent: WeakDirRef,
|
|
||||||
pub name: String,
|
|
||||||
pub size: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct Dir {
|
|
||||||
parent: Option<WeakDirRef>,
|
|
||||||
pub name: String,
|
|
||||||
children: Vec<Node>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DirRef {
|
|
||||||
//impling it on the ref makes the recursion a bit easier.
|
|
||||||
fn total_size(&self) -> usize {
|
|
||||||
self.0
|
|
||||||
.borrow()
|
|
||||||
.deref()
|
|
||||||
.children
|
|
||||||
.iter()
|
|
||||||
.map(|f| match f {
|
|
||||||
Node::Dir(dir) => dir.total_size(),
|
|
||||||
Node::File(file) => file.0.borrow().size,
|
|
||||||
})
|
|
||||||
.sum()
|
|
||||||
}
|
|
||||||
fn new(name: String) -> Self {
|
|
||||||
DirRef(Rc::new(RefCell::new(Dir {
|
|
||||||
parent: None,
|
|
||||||
name,
|
|
||||||
children: Vec::new(),
|
|
||||||
})))
|
|
||||||
}
|
|
||||||
|
|
||||||
//needs to be impled on the ref because of the need to get a weak backreference to self.
|
|
||||||
fn add(&self, node: Node) {
|
|
||||||
match node {
|
|
||||||
Node::Dir(ref dir) => {
|
|
||||||
dir.0.borrow_mut().parent = Some(WeakDirRef(Rc::downgrade(&self.0)))
|
|
||||||
}
|
|
||||||
Node::File(ref file) => file.0.borrow_mut().parent = WeakDirRef(Rc::downgrade(&self.0)),
|
|
||||||
}
|
|
||||||
self.0.borrow_mut().children.push(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl File {
|
|
||||||
fn get_parent(&self)-> &WeakDirRef {
|
|
||||||
&self.parent
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Dir {
|
|
||||||
fn get_children(&self) -> &[Node] {
|
|
||||||
&self.children
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_parent(&self) -> &Option<WeakDirRef> {
|
|
||||||
&self.parent
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,4 @@
|
||||||
#![allow(unused)]
|
use crate::fileTree::*;
|
||||||
use crate::file_tree::*;
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#![allow(unused)]
|
use crate::fileTree::*;
|
||||||
use crate::file_tree::*;
|
|
||||||
use crate::parser::*;
|
use crate::parser::*;
|
||||||
|
|
||||||
pub fn part1() -> usize {
|
pub fn part1() -> usize {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#![allow(unused)]
|
use crate::fileTree::*;
|
||||||
use crate::file_tree::*;
|
|
||||||
use crate::parser::*;
|
use crate::parser::*;
|
||||||
|
|
||||||
pub fn part2() -> usize {
|
pub fn part2() -> usize {
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
#![allow(unused)]
|
|
||||||
mod part1;
|
mod part1;
|
||||||
mod part2;
|
mod part2;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod file_tree;
|
mod fileTree;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = include_str!("./input.txt");
|
let _input = include_str!("./input.txt");
|
||||||
let structured_input = parser::parse(input);
|
let _structured_input = parser::parse(_input);
|
||||||
|
|
||||||
println!("Part One");
|
println!("Part One");
|
||||||
println!("Result: {}", part1::part1());
|
println!("Result: {}", part1::part1());
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
#![allow(unused)]
|
|
||||||
use crate::utilities::*;
|
use crate::utilities::*;
|
||||||
|
|
||||||
pub fn part1() -> usize {
|
pub fn part1() -> usize {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
#![allow(unused)]
|
|
||||||
use crate::utilities::*;
|
use crate::utilities::*;
|
||||||
|
|
||||||
pub fn part2() -> usize {
|
pub fn part2() -> usize {
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
#![allow(unused)]
|
|
||||||
mod part1;
|
mod part1;
|
||||||
mod part2;
|
mod part2;
|
||||||
mod utilities;
|
mod utilities;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = include_str!("./input.txt");
|
let _input = include_str!("./input.txt");
|
||||||
let structured_input = utilities::parse(input);
|
let _structured_input = utilities::parse(_input);
|
||||||
|
|
||||||
println!("Part One");
|
println!("Part One");
|
||||||
println!("Result: {}", part1::part1());
|
println!("Result: {}", part1::part1());
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
#![allow(unused)]
|
|
||||||
pub fn parse(input: &str) -> usize {
|
pub fn parse(input: &str) -> usize {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue