@@ -186,12 +186,20 @@ impl Word {
186186 /// assert_eq!(w2.match_code(&w1), "__GYG");
187187 /// ```
188188 pub fn match_code ( & self , w : & Word ) -> String {
189+ let mut target_chars: Vec < _ > = self
190+ . chars ( )
191+ . zip ( w. chars ( ) )
192+ . filter ( |( c1, c2) | c1 != c2)
193+ . map ( |( _c1, c2) | c2)
194+ . collect ( ) ;
195+
189196 self . chars ( )
190197 . zip ( w. chars ( ) )
191198 . map ( |( c1, c2) | {
192199 if c1 == c2 {
193200 'G'
194- } else if w. contains ( c1) {
201+ } else if let Some ( index) = target_chars. iter ( ) . position ( |c| c == & c1) {
202+ target_chars. remove ( index) ;
195203 'Y'
196204 } else {
197205 '_'
@@ -324,6 +332,7 @@ mod tests {
324332 is_match,
325333 case( "words" , "GGGGG" , "words" , true ) ,
326334 case( "abcde" , "_____" , "fghij" , true ) ,
335+ case( "steal" , "YYYYY" , "least" , true ) ,
327336 case( "choir" , "____Y" , "wrung" , true ) ,
328337 case( "child" , "_YYY_" , "light" , true ) ,
329338 case( "stole" , "YYG_G" , "those" , true ) ,
@@ -338,11 +347,46 @@ mod tests {
338347 case( "robot" , "YY__Y" , "thorn" , true ) ,
339348 case( "nylon" , "___YG" , "thorn" , true ) ,
340349 case( "tacit" , "G____" , "thorn" , true ) ,
341- case( "crate" , "__YG_" , "haste" , false )
350+ case( "crate" , "__YG_" , "haste" , false ) ,
351+ case( "abase" , "Y_Y__" , "cacao" , true ) ,
352+ case( "abaka" , "Y_Y__" , "cacao" , true ) ,
353+ case( "avian" , "Y__G_" , "cacao" , true )
342354 ) ]
343355 fn test_is_match ( input : & str , code : & str , target : & str , is_match : bool ) {
344356 let constraint_set = ConstraintSet :: try_from ( ( input, code) ) . unwrap ( ) ;
345357
346358 assert_eq ! ( constraint_set. is_match( & Word :: from( target) ) , is_match) ;
347359 }
360+
361+ #[ rstest(
362+ input,
363+ target,
364+ code,
365+ case( "words" , "words" , "GGGGG" ) ,
366+ case( "abcde" , "fghij" , "_____" ) ,
367+ case( "steal" , "least" , "YYYYY" ) ,
368+ case( "choir" , "wrung" , "____Y" ) ,
369+ case( "child" , "light" , "_YYY_" ) ,
370+ case( "stole" , "those" , "YYG_G" ) ,
371+ case( "raise" , "moist" , "__GG_" ) ,
372+ case( "slate" , "pleat" , "_GYYY" ) ,
373+ case( "blast" , "aloft" , "_GY_G" ) ,
374+ case( "raise" , "elder" , "Y___Y" ) ,
375+ case( "brink" , "robin" , "YYYY_" ) ,
376+ case( "phase" , "shake" , "_GGYG" ) ,
377+ case( "armor" , "aroma" , "GGYY_" ) ,
378+ case( "canal" , "caulk" , "GG__Y" ) ,
379+ case( "robot" , "thorn" , "YY__Y" ) ,
380+ case( "nylon" , "thorn" , "___YG" ) ,
381+ case( "tacit" , "thorn" , "G____" ) ,
382+ case( "crate" , "haste" , "__YGG" ) ,
383+ case( "abase" , "cacao" , "Y_Y__" ) ,
384+ case( "abaka" , "cacao" , "Y_Y__" ) ,
385+ case( "avian" , "cacao" , "Y__G_" )
386+ ) ]
387+ fn test_match_code ( input : & str , target : & str , code : & str ) {
388+ let word = Word :: from ( input) ;
389+
390+ assert_eq ! ( word. match_code( & Word :: from( target) ) , code) ;
391+ }
348392}
0 commit comments