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ループにパイプライン化されます|%{...}
。今回は、result-array-selectorへのインデックス付けが($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
}
Previous-er-er、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)
}