Skip to content

Commit

Permalink
Part 1 Day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
bozdoz committed Dec 22, 2023
1 parent 9c3b233 commit dfca2e4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
44 changes: 44 additions & 0 deletions BLOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
# What Am I Learning Each Day?

### Day 19

**Difficulty: 5/10 ★★★☆☆☆☆☆☆☆**

**Time: ~2 hrs**

**Run Time: ~-**

I hate parsing these things.

This was the biggest data structure I've done so far. I'm reasonably happy with being able to use `Ordering` enum for the rule comparisons:

```rust
struct Compare<'a> {
// not sure how I could make this a key of a struct
key: &'a str,
cmp: Ordering,
num: usize,
}
// later...
if let Some(test) = &rule.test {
let val = part.get(test.key).expect("thought we had this key");

if val.cmp(&test.num) == test.cmp {
cur = rule.goto;
break;
}
}
```

I still don't understand what `reduce` is, so I keep using `fold` which has a similar API to JavaScript.

I got `trim_matches` and `split` working with multiple values:

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

for workflow in workflow_str.lines() {
let mut details = workflow.split(trim).tak
```

Though I still don't get what that `&[_]` type is, and why it can't be inferred.

### Day 18

**Difficulty: 3/10 ★★★☆☆☆☆☆☆☆**
Expand Down
46 changes: 41 additions & 5 deletions day-19/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,43 @@ impl<'a> System<'a> {

Self { workflows, parts }
}

fn get_ratings(&self) -> usize {
let mut approved: Vec<&HashMap<&str, usize>> = vec![];

for part in self.parts.iter() {
// start at 'in'
let mut cur = "in";

while cur != "A" && cur != "R" {
for rule in self.workflows.get(cur).expect("don't have cur").rules.iter() {
if let Some(test) = &rule.test {
let val = part.get(test.key).expect("thought we had this key");

if val.cmp(&test.num) == test.cmp {
cur = rule.goto;
break;
}
} else {
// just goto
cur = rule.goto;
break;
}
}
}
if cur == "A" {
approved.push(part);
}
}

approved.iter().fold(0, |acc, e| {
acc + e["x"] + e["m"] + e["a"] + e["s"]
})
}
}

fn part_one() -> usize {
0
fn part_one(system: &System) -> usize {
system.get_ratings()
}

fn part_two() -> usize {
Expand All @@ -113,9 +146,11 @@ fn main() {
let start = Instant::now();
let contents = fs::read_to_string("./src/input.txt").unwrap();

let system = System::new(&contents);

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

Expand All @@ -136,9 +171,10 @@ mod tests {

#[test]
fn test_part_one() {
let ans = part_one();
let system = System::new(EXAMPLE);
let ans = part_one(&system);

assert_eq!(ans, 0);
assert_eq!(ans, 19114);
}

#[test]
Expand Down

0 comments on commit dfca2e4

Please sign in to comment.