|
| 1 | +using namespace System.Collections.Generic |
| 2 | + |
| 3 | +function Get-NewState([int]$col, [int]$carry, [int[]]$assignments, [int]$used) { |
| 4 | + [PSCustomObject]@{ |
| 5 | + ColIdx = $col |
| 6 | + Carry = $carry |
| 7 | + Assignment = $assignments |
| 8 | + Used = $used |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +function Invoke-Alphametics { |
| 13 | + <# |
| 14 | + .SYNOPSIS |
| 15 | + Implement a solver for alphametic puzzles. |
| 16 | +
|
| 17 | + .PARAMETER Puzzle |
| 18 | + The string represent the puzzle. |
| 19 | +
|
| 20 | + .NOTES |
| 21 | + This solution is not optimized enough for the biggest test. |
| 22 | + #> |
| 23 | + [CmdletBinding()] |
| 24 | + Param( |
| 25 | + [string] $Puzzle |
| 26 | + ) |
| 27 | + #Parse the puzzle: addens and sum |
| 28 | + $Words = -split $Puzzle -match "\w" |
| 29 | + $addens, $sum = $Words[0..($Words.Count-2)], $Words[-1] |
| 30 | + $maxLen = $sum.Length |
| 31 | + |
| 32 | + if ($addens | Where-Object { $_.Length -gt $maxLen }) { return $null } |
| 33 | + # Extract unique letters and index them |
| 34 | + $Letters = $Puzzle -split "" -match "[A-Z]" | Select-Object -Unique |
| 35 | + |
| 36 | + # getting leading letter and they cant be zero |
| 37 | + $isLeading = [bool[]]::new($Letters.Count) |
| 38 | + foreach ($word in $Words) { |
| 39 | + $index = $Letters.IndexOf($word[0]) |
| 40 | + $isLeading[$index] = $true |
| 41 | + } |
| 42 | + # build the grid or array of column |
| 43 | + $paddedWords = $Words | ForEach-Object {$_.PadLeft($maxLen, ' ')} |
| 44 | + $paddedaddens = $paddedWords | Select-Object -First ($Words.Count - 1) |
| 45 | + $paddedsum = $paddedWords | Select-Object -Last 1 |
| 46 | + |
| 47 | + $columns = @() |
| 48 | + for ($c = 0; $c -lt $maxLen; $c++) { |
| 49 | + $newColumn = [PSCustomObject]@{ |
| 50 | + Addens = @() |
| 51 | + Result = -1 |
| 52 | + } |
| 53 | + foreach ($word in $paddedaddens) { |
| 54 | + $letter = $word[$maxLen - 1 - $c] |
| 55 | + $newColumn.Addens += $letter -ne ' ' ? $Letters.IndexOf($letter) : @() |
| 56 | + } |
| 57 | + $newColumn.Result = $Letters.IndexOf($paddedsum[$maxLen - 1 - $c]) |
| 58 | + $columns+= $newColumn |
| 59 | + } |
| 60 | + |
| 61 | + $Letters | ForEach-Object {$allowed = [ordered]@{}} {$allowed[$_] = $isLeading[$Letters.IndexOf($_)] ? (1..9) : (0..9)} |
| 62 | + ### SOLVING |
| 63 | + [int[]]$assignments = @(1..$Letters.Count).ForEach({-1}) |
| 64 | + $usedBit = 0x400 |
| 65 | + $stack = [Stack[PSCustomObject]]::new() |
| 66 | + $state = Get-NewState 0 0 $assignments $usedBit |
| 67 | + $stack.Push($state) |
| 68 | + |
| 69 | + while ($stack.Count -gt 0) { |
| 70 | + $state = $stack.Pop() |
| 71 | + # Base case: past last column return result |
| 72 | + if ($state.ColIdx -ge $maxLen) { |
| 73 | + return 0..($state.Assignment.Count - 1) | ForEach-Object -Begin {$result = [ordered]@{}} -Process { |
| 74 | + $result[$Letters[$_]] = $state.Assignment[$_] } -End {$result} |
| 75 | + } |
| 76 | + $col = $columns[$state.ColIdx] |
| 77 | + # Find the first unassigned letter in this column |
| 78 | + $unassigned = $col.Addens | Where-Object { $state.Assignment[$_] -eq -1 } |
| 79 | + |
| 80 | + if ($unassigned.Count -eq 0) { |
| 81 | + # All addens letters in this column are assigned → calculate sum |
| 82 | + $totalSum = ($col.Addens | ForEach-Object {$state.Assignment[$_]} | Measure-Object -Sum).Sum |
| 83 | + $resultDigit = ($totalSum + $state.Carry) % 10 |
| 84 | + $newCarry = [math]::Floor(($totalSum + $state.Carry) / 10) |
| 85 | + |
| 86 | + if ($state.Assignment[$col.Result] -eq -1) { #Sum digit not assigned yet. assign and move to new col |
| 87 | + if ( (($state.Used -band (1 -shl $resultDigit)) -ne 0) -or $resultDigit -notin $allowed[$col.Result]) { continue } |
| 88 | + $newState = Get-NewState ($state.ColIdx + 1) $newCarry $state.Assignment.Clone() ($state.Used -bor (1 -shl $resultDigit)) |
| 89 | + $newState.Assignment[$col.Result] = $resultDigit |
| 90 | + $stack.Push($newState) |
| 91 | + } else { # Sum digit already assigned → check consistency |
| 92 | + if ($state.Assignment[$col.Result] -eq $resultDigit) { |
| 93 | + $newState = Get-NewState ($state.ColIdx + 1) $newCarry $state.Assignment.Clone() ($state.Used -bor (1 -shl $resultDigit)) |
| 94 | + $stack.Push($newState) |
| 95 | + } |
| 96 | + } |
| 97 | + } else { #still unassigned letter(s) |
| 98 | + $firstUnassigned = $unassigned[0] |
| 99 | + $possibleValues = $allowed[$Letters[$firstUnassigned]] | Where-Object {($state.Used -band (1 -shl $_)) -eq 0 } |
| 100 | + foreach ($value in $possibleValues) { |
| 101 | + $newState = Get-NewState $state.ColIdx $state.Carry $state.Assignment.Clone() ($state.Used -bor (1 -shl $value)) |
| 102 | + $newState.Assignment[$firstUnassigned] = $value |
| 103 | + $stack.Push($newState) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments