Skip to content

Commit

Permalink
vec instead of hashset for visited set
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaffrey committed Dec 22, 2023
1 parent e41144d commit a93a663
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 19](./src/bin/19.rs) | `83.9µs` | `90.9µs` |
| [Day 20](./src/bin/20.rs) | `323.9µs` | `1.2ms` |
| [Day 21](./src/bin/21.rs) | `64.1µs` | `494.7µs` |
| [Day 22](./src/bin/22.rs) | `252.3µs` | `773.9µs` |
| [Day 22](./src/bin/22.rs) | `249.6µs` | `564.7µs` |

**Total: 17.36ms**
**Total: 17.15ms**
<!--- benchmarking table --->

---
Expand Down
13 changes: 7 additions & 6 deletions src/bin/22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,27 @@ pub fn part_two(input: &str) -> Option<u32> {
let mut bricks = input.lines().map(Brick::from).collect::<Vec<_>>();
bricks.sort_unstable();
let tower = Tower::from_bricks(bricks);
let mut visited = FxHashSet::default();
let mut visited = vec![false; tower.bricks.len()];
let mut stack = Vec::new();
let mut fall_count = 0;
for brick in 0..tower.bricks.len() {
visited.clear();
visited.insert(brick);
visited.fill(false);
visited[brick] = true;
stack.push(brick);
while let Some(cur) = stack.pop() {
for &supported in &tower.supports[cur] {
let loose = tower.supported[supported]
.iter()
.filter(|&base| !visited.contains(base))
.filter(|&&base| !visited[base])
.count()
== 0;
if loose && visited.insert(supported) {
if loose && !visited[supported] {
visited[supported] = true;
stack.push(supported);
fall_count += 1;
}
}
}
fall_count += visited.len() - 1;
}

Some(fall_count as u32)
Expand Down

0 comments on commit a93a663

Please sign in to comment.