Skip to content

Instantly share code, notes, and snippets.

@WimObiwan
Created April 8, 2020 08:22
Show Gist options
  • Save WimObiwan/217ca92d436a6125b394e29aab52985c to your computer and use it in GitHub Desktop.
Save WimObiwan/217ca92d436a6125b394e29aab52985c to your computer and use it in GitHub Desktop.
function Match([string]$x, [string]$y) {
if ($x.Length -ne $y.Length) { return '' }
$result = ''
@(0..($x.Length - 1)) | %{
if ($x[$_] -eq $y[$_]) {
$result += '*'
} elseif ($y.Contains($x[$_])) {
$result += '?'
} else {
$result += '.'
}
}
$result
}
function Right([string]$x, [string]$y) {
$m = Match $x $y
($m.ToCharArray() -eq '*').Count
}
function Wrong([string]$x, [string]$y) {
$m = Match $x $y
($m.ToCharArray() -eq '?').Count
}
@(0..999) | ?{
(Wrong '147' $_) -eq 1 `
-and (Right '189' $_) -eq 1 `
-and (Wrong '964' $_) -eq 1 `
-and (Right '523' $_) -eq 0 -and (Wrong '523' $_) -eq 1 `
-and (Wrong '286' $_) -eq 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment