|
1 | 1 | //! # Inventory Management System
|
| 2 | + |
2 | 3 | use crate::util::hash::*;
|
3 | 4 |
|
4 | 5 | pub fn parse(input: &str) -> Vec<&[u8]> {
|
@@ -47,21 +48,17 @@ pub fn part2(input: &[&[u8]]) -> String {
|
47 | 48 | let width = input[0].len();
|
48 | 49 |
|
49 | 50 | let mut seen = FastSet::with_capacity(input.len());
|
50 | | - let mut buffer = [0; 32]; |
51 | 51 |
|
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. |
53 | 54 | for column in 0..width {
|
54 | 55 | 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..]; |
57 | 58 |
|
58 | | - if !seen.insert(buffer) { |
| 59 | + if !seen.insert([prefix, suffix]) { |
59 | 60 | // 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(); |
65 | 62 | }
|
66 | 63 | }
|
67 | 64 |
|
|
0 commit comments