Created
April 16, 2015 20:20
-
-
Save JFFail/def6d1122fcc8263079e to your computer and use it in GitHub Desktop.
Reddit Daily Programmer #210 - Integer Harmony
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
#Reddit Daily Programmer #210 - http://www.reddit.com/r/dailyprogrammer/comments/32goj8/20150413_challenge_210_easy_intharmonycom/ | |
#Function to append zeros. | |
function ConvertToBinary | |
{ | |
param($integer) | |
return [Convert]::ToString($integer, 2) | |
} | |
#Function to prefix any needed zeroes. | |
function PrefixZero | |
{ | |
param | |
( | |
$binValue, | |
$length | |
) | |
#Figure out if it needs to be 8 or 16 characters. | |
if($length -le 8) | |
{ | |
$total = 8 | |
} | |
else | |
{ | |
$total = 16 | |
} | |
#Prefix however many zeroes are needed. | |
while($binValue.Length -lt $total) | |
{ | |
$binValue = "0" + $binValue | |
} | |
return $binValue | |
} | |
#Function to determine compatibility. | |
function DetermineCompatibility | |
{ | |
param | |
( | |
$first, | |
$second | |
) | |
#Track how many values they have in common. | |
$inCommon = 0 | |
#Compare each value. | |
for($i = 0; $i -lt $first.Length; $i++) | |
{ | |
if($first[$i] -eq $second[$i]) | |
{ | |
$inCommon++ | |
} | |
} | |
#Do some math for the percentage of compatibility. | |
$compat = ($inCommon / $first.Length) * 100 | |
Write-Host $compat"% Compatibility" | |
} | |
#Function to determine the exact opposite value. | |
function FindOpposite | |
{ | |
param($value) | |
$opposite = "" | |
for($i = 0; $i -lt $value.Length; $i++) | |
{ | |
if($value[$i] -eq "1") | |
{ | |
$opposite += "0" | |
} | |
else | |
{ | |
$opposite += "1" | |
} | |
} | |
#Convert the opposite value into decimal. | |
$decOpposite = [Convert]::ToInt64($opposite, 2) | |
Write-Host "$value should avoid $decOpposite" | |
} | |
$first = 100 | |
$second = 42 | |
#Convert to binary. | |
$firstBin = ConvertToBinary -integer $first | |
$secondBin = ConvertToBinary -integer $second | |
#Prefix any needed zeroes. | |
$largest = $firstBin.Length | |
if($secondBin.Length -gt $largest) | |
{ | |
$largest = $secondBin.Length | |
} | |
$firstBin = PrefixZero -binValue $firstBin -length $largest | |
$secondBin = PrefixZero -binValue $secondBin -length $largest | |
#Figure out their compatibility. | |
DetermineCompatibility -first $firstBin -second $secondBin | |
#Show their opposites. | |
FindOpposite -value $firstBin | |
FindOpposite -value $secondBin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment