File tree Expand file tree Collapse file tree 1 file changed +20
-18
lines changed Expand file tree Collapse file tree 1 file changed +20
-18
lines changed Original file line number Diff line number Diff line change 1
1
//! # Inventory Management System
2
- use crate :: util:: hash:: * ;
3
2
4
3
pub fn parse ( input : & str ) -> Vec < & [ u8 ] > {
5
4
input. lines ( ) . map ( str:: as_bytes) . collect ( )
@@ -44,29 +43,32 @@ pub fn part1(input: &[&[u8]]) -> u32 {
44
43
}
45
44
46
45
pub fn part2 ( input : & [ & [ u8 ] ] ) -> String {
47
- let width = input[ 0 ] . len ( ) ;
46
+ // Manually compare all IDs, as it is faster than other methods considering there are so few total IDs
47
+ for i in 0 ..input. len ( ) {
48
+ for ii in i + 1 ..input. len ( ) {
49
+ let id1 = input[ i] ;
50
+ let id2 = input[ ii] ;
48
51
49
- let mut seen = FastSet :: with_capacity ( input. len ( ) ) ;
50
- let mut buffer = [ 0 ; 32 ] ;
51
-
52
- // Use a set to check for duplicates after replacing a single character with '*' in each column.
53
- for column in 0 ..width {
54
- for & id in input {
55
- buffer[ 0 ..width] . copy_from_slice ( id) ;
56
- buffer[ column] = b'*' ;
52
+ let mut diff = false ;
53
+ for ( a, b) in id1. iter ( ) . zip ( id2) {
54
+ if a != b {
55
+ if diff {
56
+ diff = false ;
57
+ break ;
58
+ }
59
+ diff = true ;
60
+ }
61
+ }
57
62
58
- if !seen . insert ( buffer ) {
59
- // Convert to String
60
- return buffer
63
+ if diff {
64
+ // Build the string of characters which are the same between both IDs
65
+ return id1
61
66
. iter ( )
62
- . filter ( | & & b| b . is_ascii_lowercase ( ) )
63
- . map ( | & b| b as char )
67
+ . zip ( id2 )
68
+ . filter_map ( | ( a , b ) | if a == b { Some ( char:: from ( * a ) ) } else { None } )
64
69
. collect ( ) ;
65
70
}
66
71
}
67
-
68
- seen. clear ( ) ;
69
72
}
70
-
71
73
unreachable ! ( )
72
74
}
You can’t perform that action at this time.
0 commit comments