Skip to content

perf: improve performance of 2018 day 2 part 2 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/year2018/day02.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! # Inventory Management System

use crate::util::hash::*;

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

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

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

if !seen.insert(buffer) {
if !seen.insert([prefix, suffix]) {
// Convert to String
return buffer
.iter()
.filter(|&&b| b.is_ascii_lowercase())
.map(|&b| b as char)
.collect();
return prefix.iter().chain(suffix).cloned().map(char::from).collect();
}
}

Expand Down
Loading