Skip to content

Commit

Permalink
partial day 19 basic setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bozdoz committed Dec 21, 2023
1 parent 8d84002 commit b3bcbdd
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
9 changes: 9 additions & 0 deletions day-19/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "day-19"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lib = { version = "0.1.0", path = "../lib" }
17 changes: 17 additions & 0 deletions day-19/src/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
px{a<2006:qkq,m>2090:A,rfg}
pv{a>1716:R,A}
lnx{m>1548:A,A}
rfg{s<537:gd,x>2440:R,A}
qs{s>3448:A,lnx}
qkq{x<1416:A,crn}
crn{x>2662:A,R}
in{s<1351:px,qqz}
qqz{s>2770:qs,m<1801:hdj,R}
gd{a>3333:R,R}
hdj{m>838:A,pv}

{x=787,m=2655,a=1222,s=2876}
{x=1679,m=44,a=2067,s=496}
{x=2036,m=264,a=79,s=2244}
{x=2461,m=1339,a=466,s=291}
{x=2127,m=1623,a=2188,s=1013}
150 changes: 150 additions & 0 deletions day-19/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use std::{ time::Instant, fs, collections::HashMap, cmp::Ordering };
use lib::get_part;

// today's the biggest struct dependencies made so far
// all with lifetimes

struct Compare<'a> {
// not sure how I could make this a key of a struct
key: &'a str,
cmp: Ordering,
num: usize,
}

struct Rule<'a> {
test: Option<Compare<'a>>,
goto: &'a str,
}

struct Workflow<'a> {
rules: Vec<Rule<'a>>,
}

struct System<'a> {
workflows: HashMap<&'a str, Workflow<'a>>,
parts: Vec<HashMap<&'a str, usize>>,
}

impl<'a> System<'a> {
fn new(contents: &'a str) -> Self {
let (workflow_str, parts_str) = contents
.split_once("\n\n")
.expect("I thought it was in two sections!");
let mut workflows = HashMap::new();

// TIL
let trim: &[_] = &['{', '}'];
let rule_symbols: &[_] = &['<', '>', ':'];

for workflow in workflow_str.lines() {
let mut details = workflow.split(trim).take(2);

let name = details.next().expect("name");
let rules = details
.next()
.expect("rules")
.split(",")
.map(|rule| {
let rule_parts = rule
.split(rule_symbols)
.collect::<Vec<&str>>();

if rule_parts.len() == 1 {
return Rule {
goto: rule_parts[0],
test: None,
};
}
let cmp = if rule.contains("<") {
Ordering::Less
} else {
Ordering::Greater
};

let test = Compare {
cmp,
key: rule_parts[0],
num: rule_parts[1].parse().expect("num"),
};

Rule {
goto: rule_parts.last().expect("goto"),
test: Some(test),
}
})
.collect();

workflows.insert(name, Workflow {
rules,
});
}

let mut parts = vec![];

for part_str in parts_str.lines() {
let mut part = HashMap::new();

part_str
.trim_matches(trim)
.split(",")
.for_each(|cat| {
let (k, v) = cat.split_once("=").expect("xmas");

part.insert(k, v.parse().expect("not a number"));
});

parts.push(part);
}

Self { workflows, parts }
}
}

fn part_one() -> usize {
0
}

fn part_two() -> usize {
0
}

fn main() {
let (one, two) = get_part();
let start = Instant::now();
let contents = fs::read_to_string("./src/input.txt").unwrap();

if one {
let now = Instant::now();
let ans = part_one();
println!("Part one: {:?} {:?}", ans, now.elapsed());
}

if two {
let now = Instant::now();
let ans = part_two();
println!("Part two: {:?} {:?}", ans, now.elapsed());
}

println!("Time: {:?}", start.elapsed())
}

#[cfg(test)]
mod tests {
use super::*;

const EXAMPLE: &str = include_str!("./example.txt");

#[test]
fn test_part_one() {
let ans = part_one();

assert_eq!(ans, 0);
}

#[test]
fn test_part_two() {
let ans = part_two();

assert_eq!(ans, 0);
}
}

0 comments on commit b3bcbdd

Please sign in to comment.