| Bytes | Lang | Time | Link |
|---|---|---|---|
| 034 | Juby | 240227T173930Z | Jordan |
| 073 | PowerShell Core | 240301T000104Z | Julian |
| 014 | Uiua SBCS | 240221T060034Z | chunes |
| 047 | Factor + math.unicode | 220309T003607Z | chunes |
| 040 | Ruby | 200605T141625Z | Dingus |
| 046 | Wolfram Language Mathematica | 200607T131529Z | LegionMa |
| 044 | APLNARS | 200709T023356Z | user5898 |
| 045 | Raku | 200708T044123Z | Jo King |
| 050 | Wolfram Language Mathematica | 200708T025134Z | att |
| 009 | Gaia | 200605T185125Z | Giuseppe |
| 078 | PHP | 200608T095213Z | Kaddath |
| 058 | Perl 5 pl | 200605T175822Z | Kjetil S |
| 090 | Java JDK | 200608T083935Z | Olivier |
| 066 | Retina | 200606T214034Z | math jun |
| 006 | Japt h | 200606T094845Z | Shaggy |
| 010 | Pyth | 200607T135546Z | Sok |
| 005 | 05AB1E | 200605T131115Z | ovs |
| 059 | Io | 200606T084339Z | user9206 |
| 014 | K Kona | 200606T072913Z | Galen Iv |
| 016 | J | 200605T162623Z | xash |
| 014 | APL Dyalog Unicode | 200606T000504Z | RGS |
| 057 | R | 200605T201156Z | Dominic |
| 007 | Jelly | 200605T134055Z | Kevin Cr |
| 063 | JavaScript ES6 | 200605T154626Z | Arnauld |
| 021 | Charcoal | 200605T153812Z | Neil |
| 079 | perl M5.010 a | 200605T153657Z | Abigail |
| 057 | Python 2 | 200605T134540Z | ovs |
J-uby, 34 bytes
:!~&:|%(~:each_cons&2|:*&(+:-|:|))
Explanation
:!~ & :| % (~:each_cons & 2 | :* & (+:- | :|))
:!~ & # Until fixed point...
:| % ( ) # Union of input and...
+:- | :| # absolute differences of...
~:each_cons & 2 | :* & ( ) # consecutive pairs
PowerShell Core, 73 bytes
for($t=$args;$t[$i+2]){$t+=,($d=$t[$i++]-$t[$i]-replace'-')*!($d-in$t)}$t
Uiua SBCS, 14 bytes
⍥(◴⊂:⌵↘1°\+.)∞
⍥(◴⊂:⌵↘1°\+.)∞
⍥( )∞ # to fixed point
. # duplicate
↘1°\+ # differences
⌵ # abs
⊂: # append
◴ # deduplicate
Factor + math.unicode, 47 bytes
[ [ dup differences vabs ∪ ] to-fixed-point ]
Explanation
[ ... ] to-fixed-pointDo[ ... ]to the input until it stops changing.
! { 16 21 11 2 }
dup ! { 16 21 11 2 } { 16 21 11 2 }
differences ! { 16 21 11 2 } { 5 -10 -9 }
vabs ! { 16 21 11 2 } { 5 10 9 }
∪ ! { 16 21 11 2 5 10 9 } (set union)
! (etc...)
Ruby, 40 bytes
->a,i=0{a|=[(p(a[i])-a[i+=1]).abs];redo}
Takes an array a as input and adds unique new elements to it using the set append operator |=. Terminates by throwing a rescuable TypeError (which is now allowed) when attempting to subtract beyond the last element of the array. By that point all of the required output has already been printed to STDOUT.
Ruby, 48 bytes
->a,i=0{a|=[(p(A).-a[i+=1]||0).abs]while A=a[i]}
This version avoids the TypeError at a cost of 8 bytes.
Wolfram Language (Mathematica), 57 51 46 bytes
-11 bytes thanks to att!
#//.a_:>Keys@Counts@Join[a,Abs@Differences@a]&
Try it online! Pure function. Takes a list as input and returns another list as output. It just repeatedly appends the absolute differences to the end of the list and removes duplicates, until the list no longer changes.
APL(NARS), 22 chars, 44 bytes
{⍬≡k←∪⍵∼⍨∣2-/⍵:⍵⋄∇⍵,k}
test:
f←{⍬≡k←∪⍵∼⍨∣2-/⍵:⍵⋄∇⍵,k}
f 16 21 11 2
16 21 11 2 5 10 9 3 1 6
f 2 3 4 5
2 3 4 5 1
Raku, 45 bytes
{|$_,{|keys abs([-] @_[++$,$++])∖@_}...^!*}
Explanation
{ } # Anonymous codeblock
... # Generating a sequence
|$_, # Starting with the input
{ } # Where we add to the sequence
abs([-] ) # The absolute difference
@_[++$,$++] # Of the next pair of elements
|keys ∖@_ # Set subtracting the sequence
...!* # Continue until we run out of pairs
Wolfram Language (Mathematica), 50 bytes
#//.l:_[___,b_,a_,___]:>l<>d/;FreeQ[l,d=Abs[b-a]]&
Takes a sequence with any head. If the sequence is already complete, returns it unchanged. Otherwise, returns the completed sequence in a StringJoin.
<> (StringJoin) is short, flattens Lists, does not operate on integer (non-string) arguments, and is Flat. It's also not Orderless, so can store ordered data.
_[ ] (* match a nonatomic expression with any head, *)
l: (* and name it l. it contains: *)
b_,a_, (* two adjacent numbers, b,a, *)
___, ___ (* as close to the front as possible, *)
/; (* subject to the condition that *)
FreeQ[l,d=Abs[b-a] (* |b-a| is not in l. *)
#//. :>l<>d (* while the condition holds, append |b-a|. *)
Gaia, 9 bytes
e⟨:ọȯ¦|⟩°
Note that the documentation states that ȯ is sign of z when z is a numeric type, but it's actually abs...
e # eval input as Gaia code (push as a list)
⟨ ⟩° # until there's no change:
: # dup the list
ọ # take the differences
ȯ¦ # take the absolute values
| # and set union
PHP, 96 78 bytes
for($a=$argv;$n=$a[++$i];print"$n ")in_array($v=abs($n-$a[$i+1]),$a)?:$a[]=$v;
EDIT: 18 bytes saved with the help of Domenico Modica, thanks!
Perl 5 -pl, 58 bytes
$d=abs$a[$;]-$a[++$;],/\b$d\b/||s/$/ $d/ while(@a=split)>$
Ungolfed:
# with -pl the special var $_ is initialized with the input line string
while(
@a = split # array @a = the numbers currently in the $_ special var
and $i++ < @a # and the $i index (counter) is less than the length of @a
# in the golfed version $; is used instead of $i
){
$d = abs $a[$i] - $a[$i-1] # $d = non-zero absolute diff btw element $i and $i-1
and !/\b$d\b/ # and $d unseen before? \b is "borders" around digits
and s/$/ $d/ # and if so append space and $d to $_
}
# with -pl the current $_ and \n is printed
Run:
$ echo 16 21 11 2 | perl -pl program_58_bytes.pl
16 21 11 2 5 10 9 3 1 6
Java (JDK), 90 bytes
l->{for(int i=0,v;i+1<l.size();)if(!l.contains(v=Math.abs(l.get(i++)-l.get(i))))l.add(v);}
Retina, 84 68 66 bytes
Edit: Saved 16 18 bytes thanks to @Neil !! This answer is now mildly competitive :P
\d+
*
^
#
{`#( _+)(_*)(\1(_*).*)
$1$2#$3 $2$4
)D` _+
^ |#
_+
$.&
Not the shortest answer to this question, but it was fun to golf anyway :)
Explanation
\d+
*
Convert each input number to unary (using _'s)
^
#
Insert a # at the start of the input
{`
)`
Run the following 2 stages in a loop until the input stops changing:
#( _+)(_*)(\1(_*).*)
$1$2#$3 $2$4
Take the two numbers to the right of the # and remove the maximal number of _'s from each. This results in the absolute difference between the two numbers. Append this result to the end of the list.
D` _+
Deduplicate. If the number that was just added matches a number already in the list, remove it.
^ |#
_+
$.&
Once the loop breaks, remove the # and leading space, and convert back to decimal
Japt -h, 10 6 bytes
£=âUäa
£=âUäa :Implicit input of array U
£ :Map
= :Reassign to U, for the next iteration
â :Setwise union of U and
Uä :Consecutive pairs of U
a : Reduced by absolute difference
Pyth, 10 bytes
u{+GaM.:G2
u{+GaM.:G2
u Apply inner function until a repeat is found, current value G, starting with input
.:G2 Find all sublists of length 2
aM Absolute difference between each pair
+G Append to G
{ Deduplicate
Implicit print
05AB1E, 6 5 bytes
Δ¥Ä«Ù
Δ until the output doesn't change:
¥Ä absolute differences
« concatenate to the original input
Ù only keep unique values
Io, 59 bytes
Port of the Python 2 answer.
method(x,a :=0;x foreach(i,x=x push((i-a)abs)unique;a=i);x)
J, 17 16 bytes
(~.@,2|@-/\])^:_
How it works
(~.@,2|@-/\])^:_
( )^:_ until output does not change
2 /\] for each neighboring pair
|@- get the absolute difference
, append the result to the list
~.@ and remove duplicates
APL (Dyalog Unicode), 14 bytes SBCS
{⍵,⍵~⍨|2-/⍵}⍣≡
Same method as the J answer; I came up with this on my own :) also ended up being 3 bytes shorter, at least atm.
R, 68 57 bytes
v=scan();while(any(F-(F=v)))v=unique(c(v,abs(diff(v))));v
Edit: -9 bytes thanks to Giuseppe, and -2 more from (F-(F=v)) (had to be tested to see whether that would work...)
Boringly follows the instructions in the question, in the same order...
Jelly, 8 7 bytes
;IAQƲÐL
-1 byte thanks to @JonathanAllan.
Explanation:
# Full program taking a single list argument
ÐL # Repeat until the result no longer changes,
Ʋ # using the previous four links as monads:
I # Get the forward differences of the current list
A # Take their absolute values
; # Merge it to the current list
Q # And uniquify it
# (after which it is output implicitly as result)
JavaScript (ES6), 63 bytes
f=(a,i=0)=>(v=a[i]-a[++i])?f([...new Set(a).add(v>0?v:-v)],i):a
Charcoal, 21 bytes
W⁻Eθ↔⁻κ∧λ§θ⊖λθFι⊞θκIθ
Try it online! Link is to verbose version of code. Explanation:
W⁻Eθ↔⁻κ∧λ§θ⊖λθ
Generate the absolute differences between adjacent elements of the input and filter out those appearing in the input.
Fι⊞θκ
While there were any new values, push them to the input and repeat.
Iθ
Output the result.
perl -M5.010 -a, 79 bytes
@F{@F}=@F;{$d=abs($F[0]-$F[1]);$F{$d}++or$F[@F]=$d;say shift@F;@F>1&&redo}say@F
Reads in a line of input, with the integers space separated. Outputs the sequence with each number on a different line.
How does it work? It gets the input in the array @F (due to the -a command line argument). In the hash %F it stores the numbers already in @F (here we use the given that all numbers in sequence are positive integers†). Then, in a loop, we find the distance between the first two elements of @F; if not seen before, we add it to @F (and %F). We then remove the first element of @F and print it. We exit the loop if only one element is left, which is printed just before exiting the program.
†The sequence can never contain a zero, as that requires two subsequent elements to be the same, but that is not allowed.