Advertisement
Yevrag35

Word Unscrambler

Oct 30th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [string[]]$wordList = @(
  2.     'spade',
  3.     'curve',
  4.     'axiomatic',
  5.     'intelligent',
  6.     'skin',
  7.     'verdant',
  8.     'acrid',
  9.     'legs',
  10.     'moaning',
  11.     'error',
  12.     'low',
  13.     'object'
  14. )
  15.  
  16. Function Get-Seed()
  17. {
  18.     Process
  19.     {
  20.         $rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
  21.         [byte[]]$rBytes = New-Object byte[] 4
  22.         $rng.GetBytes($rBytes)
  23.         [System.BitConverter]::ToUInt32($rBytes, 0)
  24.     }
  25.     End
  26.     {
  27.         $rng.Dispose()
  28.     }
  29. }
  30.  
  31. Function Randomize-Word([string]$word)
  32. {
  33.     $entries = @{}
  34.     [char[]]$chars = $word.ToCharArray()
  35.     foreach ($c in $chars)
  36.     {
  37.         $seed = [long]::MinValue
  38.         while (-not $entries.ContainsKey($seed))
  39.         {
  40.             $seed = Get-Seed
  41.             $entries.Add($seed, $c)
  42.         }
  43.     }
  44.     [char[]]$sorted = $entries.GetEnumerator() | Sort Key | Select -ExpandProperty Value
  45.     $sorted -join ''
  46. }
  47.  
  48. [string[]]$ScrambledWords = foreach ($w in $wordList)
  49. {
  50.     Randomize-Word -word $w
  51. }
  52.  
  53. $ScrambledWords -join ', '
  54. $CorrectWords = $null
  55.  
  56. #Main foreach scrambled word
  57. foreach ($Scramble in $ScrambledWords) {
  58.     #Grab the length of the scrambled word
  59.     $PossibleWords = $wordlist | Where-Object { $_.Length -eq $Scramble.Length }
  60.         #foreach to compare possible words with the current scrambled word
  61.         foreach ($Word in $PossibleWords) {  
  62.           #turn both strings to arrays and compare them for only matches  
  63.           $Compare = Compare-Object -ReferenceObject $Word.ToCharArray() -DifferenceObject $Scramble.ToCharArray() -IncludeEqual -ExcludeDifferent
  64.             #Grab only words that have the same amount of matched letters as the scrambled word
  65.             if($Compare.Length -eq $Scramble.Length) {
  66.                 $CorrectWords += "$word, "
  67.                 }
  68.  
  69.         }
  70. }
  71.  
  72. Write-host $CorrectWords
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement