Skip to content

Commit

Permalink
submit day 22 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastil committed Feb 7, 2024
1 parent 7782df1 commit bfe581f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 19](./src/bin/19.rs) | `239.3µs` | `265.0µs` |
| [Day 20](./src/bin/20.rs) | `1.8ms` | `8.1ms` |
| [Day 21](./src/bin/21.rs) | `2.4ms` | `2.6ms` |
| [Day 22](./src/bin/22.rs) | `712.5µs` | `-` |
| [Day 22](./src/bin/22.rs) | `584.8µs` | `5.9ms` |

**Total: 1981.43ms**
**Total: 1987.20ms**

<!--- benchmarking table --->

Expand Down
65 changes: 58 additions & 7 deletions src/bin/22.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
cell::RefCell,
collections::HashMap,
collections::{HashMap, HashSet},
hash::Hash,
rc::{Rc, Weak},
};

Expand All @@ -9,13 +10,13 @@ advent_of_code::solution!(22);
pub fn part_one(input: &str) -> Option<u16> {
let brick_stack = BrickStack::from_input(input);

let redundant_bricks_count = brick_stack.redundant_bricks_count();

Some(redundant_bricks_count)
Some(brick_stack.redundant_bricks_count())
}

pub fn part_two(input: &str) -> Option<u32> {
None
let brick_stack = BrickStack::from_input(input);

Some(brick_stack.dependants_count())
}

struct BrickStack {
Expand Down Expand Up @@ -72,6 +73,43 @@ impl BrickStack {
.filter(|brick| brick.is_redundant())
.count() as u16
}

fn dependants_count(&self) -> u32 {
self.bricks
.iter()
.map(|brick| {
let mut dependants =
HashSet::from_iter(brick.supported_by.iter().map(|brick| brick.hashable()));

Self::dependants_count_recursive(brick, &mut dependants) - 1
})
.sum()
}

fn dependants_count_recursive(
brick: &Rc<Brick>,
dependants: &mut HashSet<HashableBrick>,
) -> u32 {
if brick
.supported_by
.iter()
.any(|brick| !dependants.contains(&brick.hashable()))
{
return 0;
}

dependants.insert(brick.hashable());

let count = brick
.supports
.borrow()
.iter()
.map(|brick| Self::dependants_count_recursive(&brick.upgrade().unwrap(), dependants))
.sum::<u32>()
+ 1;

count
}
}

struct Brick {
Expand Down Expand Up @@ -133,6 +171,13 @@ impl Brick {
.iter()
.all(|brick| brick.upgrade().unwrap().supported_by.len() > 1)
}

fn hashable(&self) -> HashableBrick {
HashableBrick {
start: self.start.clone(),
end: self.end.clone(),
}
}
}

impl PartialEq for Brick {
Expand All @@ -141,7 +186,13 @@ impl PartialEq for Brick {
}
}

#[derive(Hash, Eq, PartialEq)]
#[derive(Hash, Eq, PartialEq, Clone)]
struct HashableBrick {
start: Position,
end: Position,
}

#[derive(Hash, Eq, PartialEq, Clone)]
struct Position {
x: i16,
y: i16,
Expand Down Expand Up @@ -177,6 +228,6 @@ mod tests {
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(7));
}
}

0 comments on commit bfe581f

Please sign in to comment.