Skip to content

Commit a8a3007

Browse files
perf: improve performance of 2018 day 2 part 2 (#11)
* perf: improve performance of 2018 day 2 part 2 * refactor: revert back to the original approach - but only insert the prefix and suffix of IDs into the hashset
1 parent 558e7a9 commit a8a3007

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/year2018/day02.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! # Inventory Management System
2+
23
use crate::util::hash::*;
34

45
pub fn parse(input: &str) -> Vec<&[u8]> {
@@ -47,21 +48,17 @@ pub fn part2(input: &[&[u8]]) -> String {
4748
let width = input[0].len();
4849

4950
let mut seen = FastSet::with_capacity(input.len());
50-
let mut buffer = [0; 32];
5151

52-
// Use a set to check for duplicates after replacing a single character with '*' in each column.
52+
// Use a set to check for duplicates by comparing the prefix and suffix of IDs excluding one
53+
// column at a time.
5354
for column in 0..width {
5455
for &id in input {
55-
buffer[0..width].copy_from_slice(id);
56-
buffer[column] = b'*';
56+
let prefix = &id[..column];
57+
let suffix = &id[column + 1..];
5758

58-
if !seen.insert(buffer) {
59+
if !seen.insert([prefix, suffix]) {
5960
// Convert to String
60-
return buffer
61-
.iter()
62-
.filter(|&&b| b.is_ascii_lowercase())
63-
.map(|&b| b as char)
64-
.collect();
61+
return prefix.iter().chain(suffix).cloned().map(char::from).collect();
6562
}
6663
}
6764

0 commit comments

Comments
 (0)