Skip to content

Commit

Permalink
perf(18/2024): optimize by pre-allocation memory for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Dec 20, 2024
1 parent 5165099 commit 2282c0c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| [Day 15: Warehouse Woes](src/solutions/year2024/day15.rs) | ⭐⭐ | 7.226 | 9.084 |
| [Day 16: Reindeer Maze](src/solutions/year2024/day16.rs) || 6.478 | - |
| [Day 17: Chronospatial Computer](src/solutions/year2024/day17.rs) | - | - | - |
| [Day 18: RAM Run](src/solutions/year2024/day18.rs) | ⭐⭐ | 2.487 | 379.772 |
| [Day 18: RAM Run](src/solutions/year2024/day18.rs) | ⭐⭐ | 2.487 | 204.885 |
| [Day 19: Linen Layout](src/solutions/year2024/day19.rs) | ⭐⭐ | 2.923 | 22.751 |

# 2023
Expand Down
13 changes: 10 additions & 3 deletions src/solutions/year2024/day18.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ impl Solution for Day18 {
let start = self.surface.top_left_corner();
let end = self.surface.bottom_right_corner();

let mut skipped: HashSet<Point> = byte_positions
let mut skipped = HashSet::with_capacity(byte_positions.len());

let new: HashSet<Point> = byte_positions
.clone()
.into_iter()
.take(self.memory_size)
.collect();

skipped.extend(new);

#[allow(clippy::needless_range_loop)]
for i in self.memory_size..byte_positions.len() {
let current = byte_positions[i];
Expand Down Expand Up @@ -80,8 +84,11 @@ impl Day18 {
}

fn is_reachable(&self, blocked: &HashSet<Point>, start: Point, end: Point) -> bool {
let mut visited = HashSet::new();
let mut queue = vec![start];
let mut visited = HashSet::with_capacity(self.surface.area());
let mut queue = Vec::with_capacity(self.surface.area());

queue.push(start);
visited.insert(start);

while let Some(current) = queue.pop() {
if current == end {
Expand Down

0 comments on commit 2282c0c

Please sign in to comment.