##PowerShell
PowerShell
$c=2 # Set variable = 2
Write-Host ("$c + $c = " + $(2+($c++,$c--)[$c-eq2])) # Perform the addition and output
Write-Host ("c = $c") # Show that $c is still = 2
Output:
PS C:\Tools\Scripts\golfing> .2円-plus-2.ps1
2 + 2 = 5
c = 2
What the deuce?
Hint 1:
First, understand that the code
(a, b)[c](replacing a,b,c for values/variables) is a pseudo-ternary in PowerShell, where the dynamic array(a, b)is indexed into based on the valuec.
Hint 2:
Next, understand that
$true = 1and$false = 0and that arrays are zero-indexed. Thus, depending upon whether$c-eq2("$c is equal to 2") isTrueorFalse, we'll choose either the second or first value, respectively, and add that on to the2.
Hint 3:
The dynamic array gets calculated before indexing.
Explanation:
The increment and decrement don't get evaluated twice. Instead, they're evaluated before the rest of the expression. Since we're creating a dynamic array, the first element of the array is processed as
$cand saved as2, and then the post-increment happens and sets$c=$c+1=3. The second element is processed as$cand saved as3(since$cwas just set to3), and then post-decremented back to2. This makes our array(2,3). We index into that with$c-eq2... well,$cis now2again, so that evaluates to$true, or1, and so selects the second element of the array,3. Thus2+3=5. And, since$cwas decremented back down, we get the bonus of being able to outputc = 2still.
##PowerShell
$c=2 # Set variable = 2
Write-Host ("$c + $c = " + $(2+($c++,$c--)[$c-eq2])) # Perform the addition and output
Write-Host ("c = $c") # Show that $c is still = 2
Output:
PS C:\Tools\Scripts\golfing> .2円-plus-2.ps1
2 + 2 = 5
c = 2
What the deuce?
Hint 1:
First, understand that the code
(a, b)[c](replacing a,b,c for values/variables) is a pseudo-ternary in PowerShell, where the dynamic array(a, b)is indexed into based on the valuec.
Hint 2:
Next, understand that
$true = 1and$false = 0and that arrays are zero-indexed. Thus, depending upon whether$c-eq2("$c is equal to 2") isTrueorFalse, we'll choose either the second or first value, respectively, and add that on to the2.
Hint 3:
The dynamic array gets calculated before indexing.
Explanation:
The increment and decrement don't get evaluated twice. Instead, they're evaluated before the rest of the expression. Since we're creating a dynamic array, the first element of the array is processed as
$cand saved as2, and then the post-increment happens and sets$c=$c+1=3. The second element is processed as$cand saved as3(since$cwas just set to3), and then post-decremented back to2. This makes our array(2,3). We index into that with$c-eq2... well,$cis now2again, so that evaluates to$true, or1, and so selects the second element of the array,3. Thus2+3=5. And, since$cwas decremented back down, we get the bonus of being able to outputc = 2still.
PowerShell
$c=2 # Set variable = 2
Write-Host ("$c + $c = " + $(2+($c++,$c--)[$c-eq2])) # Perform the addition and output
Write-Host ("c = $c") # Show that $c is still = 2
Output:
PS C:\Tools\Scripts\golfing> .2円-plus-2.ps1
2 + 2 = 5
c = 2
What the deuce?
Hint 1:
First, understand that the code
(a, b)[c](replacing a,b,c for values/variables) is a pseudo-ternary in PowerShell, where the dynamic array(a, b)is indexed into based on the valuec.
Hint 2:
Next, understand that
$true = 1and$false = 0and that arrays are zero-indexed. Thus, depending upon whether$c-eq2("$c is equal to 2") isTrueorFalse, we'll choose either the second or first value, respectively, and add that on to the2.
Hint 3:
The dynamic array gets calculated before indexing.
Explanation:
The increment and decrement don't get evaluated twice. Instead, they're evaluated before the rest of the expression. Since we're creating a dynamic array, the first element of the array is processed as
$cand saved as2, and then the post-increment happens and sets$c=$c+1=3. The second element is processed as$cand saved as3(since$cwas just set to3), and then post-decremented back to2. This makes our array(2,3). We index into that with$c-eq2... well,$cis now2again, so that evaluates to$true, or1, and so selects the second element of the array,3. Thus2+3=5. And, since$cwas decremented back down, we get the bonus of being able to outputc = 2still.
##PowerShell
$c=2 # Set variable = 2
Write-Host ("$c + $c = " + $(2+($c++,$c--)[$c-eq2])) # Perform the addition and output
Write-Host ("c = $c") # Show that $c is still = 2
Output:
PS C:\Tools\Scripts\golfing> .2円-plus-2.ps1
2 + 2 = 5
c = 2
What the deuce?
Hint 1:
First, understand that the code
(a, b)[c](replacing a,b,c for values/variables) is a pseudo-ternary in PowerShell, where the dynamic array(a, b)is indexed into based on the valuec.
Hint 2:
Next, understand that
$true = 1and$false = 0and that arrays are zero-indexed. Thus, depending upon whether$c-eq2("$c is equal to 2") isTrueorFalse, we'll choose either the second or first value, respectively, and add that on to the2.
Hint 3:
The dynamic array gets calculated before indexing.
Explanation:
The increment and decrement don't get evaluated twice. Instead, they're evaluated before the rest of the expression. Since we're creating a dynamic array, the first element of the array is processed as
$cand saved as2, and then the post-increment happens and sets$c=$c+1=3. The second element is processed as$cand saved as3(since$cwas just set to3), and then post-decremented back to2. This makes our array(2,3). We index into that with$c-eq2... well,$cis now2again, so that evaluates to$true, or1, and so selects the second element of the array,3. Thus2+3=5. And, since$cwas decremented back down, we get the bonus of being able to outputc = 2still.