-
-
Save IISResetMe/94140cf83a5a4fe77aa9ab86ad8cfaf5 to your computer and use it in GitHub Desktop.
Advent of PowerShell 2018, Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string[]]$IDs | |
) | |
# Prepare our char count function | |
$TestCharExactCount = { | |
return @($_.ToCharArray() |Group-Object -NoElement).Where({$_.Count -eq $n}).Count -gt 0 | |
} | |
# Create two new closures over $n = 2 and $n = 3 | |
$n = 2 | |
$TestExactly2 = $TestCharExactCount.GetNewClosure() | |
$n = 3 | |
$TestExactly3 = $TestCharExactCount.GetNewClosure() | |
# Count occurences | |
$2s = $IDs.Where($TestExactly2).Count | |
$3s = $IDs.Where($TestExactly3).Count | |
# Multiplly counts for final result | |
return $2s * $3s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string[]]$IDs | |
) | |
:IDSearch | |
for($i = 0; $i -lt $IDs.Count - 1; $i++){ | |
for($j = $i + 1; $j -lt $IDs.Count; $j++){ | |
$HammingDistance = Get-HammingDistance -a $IDs[$i] -b $IDs[$j] | |
if($HammingDistance -eq 1){ | |
$ID1,$ID2 = $IDs[$i,$j] | |
break IDSearch | |
} | |
} | |
} | |
$sharedChars = for($charIdx = 0; $charIdx -lt $ID1.Length;$charIdx++){ | |
if($ID1[$charIdx] -eq $ID2[$charIdx]){ | |
$ID1[$charIdx] | |
} | |
} | |
return -join $SharedChars |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment