PowerShell中,261 190 121 95个字节
$(do{Measure-Command{$l=read-host};$l}while($l))|%{($_,(sleep -m($_.Ticks/1e4)))[($b=!$b+!$_)]}
TessellatngHeckler和tomkandy的高尔夫协助和灵感支柱
这在概念上与下面的121字节版本非常相似,我们只是动态创建和构建对象列表,而不是通过while循环将它们存储到显式数组中$a
。在这两种情况下,该对象列表都通过管道传递到相同的foreach循环中|%{...}
。这次($b=!$b+!$_)
制定了对结果数组选择器的索引,以消除if($_){$_}
下面的迭代,从而节省了更多的字节。
上一页,121字节
$l,$a=1,@();while($l){$t=Measure-Command{$l=read-host};$a+=$t,$l}$a|%{($(if($_){$_}),(sleep -m($_.Ticks/1e4)))[($b=!$b)]}
扩展并解释:
$l,$a=1,@() # Set variable $l and create array $a
while($l){ # So long as we don't have a blank line
$t=Measure-Command{$l=read-host} # Read the input and measure time to input
$a+=$t,$l # Add those values into the array
}
$a|%{ # For each item in $a, do
($(if($_){$_}),(sleep -m($_.Ticks/1e4)))[($b=!$b)]
# Magic happens here ... first, we set $b to the NOT of it's uninitialized
# value, so $b is initially set to truthy
# This value in [...] selects which of the two elements ( , ) get selected
# Truthy to start means the second command, sleep, gets chosen first, and
# then it alternates every next item, so it sleeps, then prints, then
# sleeps, then prints, etc., until we run out of $a
}
上一个190字节
function f {param($m)sleep -m $a[$m].totalmilliseconds}$a=1,1;while($a[-1]-ne""){$a+=Measure-Command{$b=read-host};$a+=$b}if(!($a[3])){f 2;exit}$i=2;while($i-lt$a.length){f($i++);$a[($i++)]}
function f { # Define a new function
param($m) # with $m as input
sleep -m $a[$m].totalmilliseconds # sleep for $a[$m] milliseconds
}
$a=1,1 # Create new array with two elements
while($a[-1]-ne""){ # While the last element isn't empty
$a+=Measure-Command{$b=read-host} # Read into $b and measure how long that took,
# and add the time into $a
$a+=$b # Then add the input into $a
}
if(!($a[3])){ # If the third element is empty, the user entered
# a blank as the only input, so...
f 2 # sleep for $a[2] ms (how long it took them to hit enter)...
exit # and exit the script
} # Else ...
$i=2 # Set a counter variable
while($i-lt$a.length){ # While we haven't reached the end of $a
f($i++) # Sleep
$a[($i++)] # Write the output
}
上一个261字节
$a=$d=@();$d+=,@(date);$x=Read-Host
while($x){$a+=,@($x);$d+=,@(date);$x=Read-Host}
if($x){0..($a.Length-1)|%{sleep -m((($d[$_+1]).ticks-($d[$_]).ticks)/1e4);$a[$_]};sleep -m((($d[-1]).ticks-($d[-2]).ticks)/1e4)}
else{sleep -m(((date).Ticks-($d[0]).Ticks)/1e4)}
神圣的冗长,蝙蝠侠!让我们分解一下:
$a=$d=@() # Create two empty arrays
$d+=,@(date) # Add the current time into $d
$x=Read-Host # Read the first line
while($x){ # So long as it's not empty
$a+=,@($x) # Add it into our output array
$d+=,@(date) # Add the current time into $d
$x=Read-Host # Get the next line
}
if($a){ # So long as $a exists (i.e., the first input wasn't blank)
0..($a.Length-1)|%{ # For-loop over the length
# Sleep for how long it took to do input
sleep -m((($d[$_+1]).ticks-($d[$_]).ticks)/1e4)
$a[$_] # Print out the input
}
# Sleep the length it took for the final blank
sleep -m((($d[-1]).ticks-($d[-2]).ticks)/1e4)
}
else{
# If we're here, the initial input was blank, so just sleep
sleep -m(((date).Ticks-($d[0]).Ticks)/1e4)
}