| Bytes | Lang | Time | Link |
|---|---|---|---|
| 024 | Juby | 250505T132729Z | Jordan |
| 003 | Thunno 2 | 230827T164616Z | The Thon |
| 012 | Desmos | 220507T043128Z | Aiden Ch |
| 007 | J | 220305T100653Z | sinvec |
| 035 | Halfwit | 220305T094216Z | emanresu |
| 004 | Japt m | 210709T072714Z | Shaggy |
| 003 | Vyxal | 210709T054848Z | emanresu |
| 016 | ARM Thumb | 210708T091554Z | Bubbler |
| 026 | Factor | 210519T230349Z | chunes |
| 018 | Zsh | 210212T174848Z | pxeger |
| 003 | Jelly | 161125T031746Z | Dennis |
| 017 | Wonder | 161129T212108Z | Mama Fun |
| 011 | ised | 161129T185932Z | orion |
| 019 | Haskell | 161125T100839Z | Renzeee |
| 005 | Dyalog APL | 161128T185612Z | ngn |
| 024 | Labyrinth | 161128T170203Z | Martin E |
| 034 | Labyrinth | 161128T154058Z | Robert H |
| nan | Perl | 161126T180402Z | Denis Ib |
| 030 | Python 2 | 161125T033038Z | Dennis |
| 008 | Mathematica | 161125T032535Z | Greg Mar |
| 009 | Pushy | 161125T211907Z | FlipTack |
| 008 | APL | 161125T203826Z | marinus |
| 048 | PHP | 161125T181456Z | user5917 |
| 016 | CJam | 161125T174337Z | Linnea G |
| 036 | C++14 | 161125T171724Z | Karl Nap |
| 076 | BrainFlak | 161125T154902Z | Riley |
| 016 | Perl 6 | 161125T151138Z | Brad Gil |
| 023 | Ruby | 161125T135335Z | G B |
| 004 | 05AB1E | 161125T091127Z | Emigna |
| 021 | JavaScript ES6 | 161125T075821Z | edc65 |
| 017 | R | 161125T033945Z | Jonathan |
| 004 | Pyke | 161125T073737Z | Blue |
| 005 | Pyth | 161125T033305Z | Steven H |
| 044 | Python | 161125T032104Z | DJMcMayh |
Thunno 2, 3 bytes
Ḍh_
Port of Dennis's Jelly answer.
Explanation
Ḍh_ # Implicit input
Ḍ # Double each number
h # Take the first item
_ # Subtract input from this
# Implicit output
Halfwit, 3.5 bytes
+><iN+
Dennis port.
+ # Double the input, adding it to itself
><i # Get the first one
N+ # Subtract from the input
Once again, I'm reminded of how annoying Halfwit's minimalism is to program with.
Vyxal, 3 bytes
dh-
Dennis port
d # Double the array
h # Take the first
- # Subtract
Or, by implementing the original spec,
Vyxal 2.5, 5 bytes
¯?hp¦
¯ # Deltas (negated, this is a bug, and will be fixed, that is why it's 2.5)
?hp # Prepend the first item of input
¦ # Take the cumulative sum
ARM (Thumb), 16 bytes
.section .text
.global func
.thumb
// void func(int* arr, int len)
// arr[i] = 2*arr[0] - arr[i] for each i
// r0 = arr, r1 = len, r2 = arr[0], r3 = arr[i]
func:
ldr r2, [r0, #0] // 6802 int head = *arr;
.loop: // do {
ldr r3, [r0, #0] // 6803 int cur = *arr;
rsb r3, r3, r2, lsl #1 // ebc3 0342 cur = (head << 1) - cur;
stmia r0!, {r3} // c008 *arr++ = cur;
sub r1, #1 // 3901 len -= 1;
bne .loop // d1f9 } while (len != 0);
bx lr // 4770 return;
Uses the formula, which nicely fits into exactly four registers in use. The stm tip was used to overwrite the value and advance the pointer in a single narrow instruction, and a wide rsb instruction was used to evaluate exactly what we want. (I know it's possible to do with a 3-operand sub and then an add for the same byte count, but I figured a single instruction would add some coolness.)
Jelly, 7 3 bytes
ḤḢ_
Background
The deltas of (a, b, c, d) are b - a, c - b, and d - c. Cumulatively reducing (a, b - a, c - b, d - c) by subtraction yields a - (b - a) = 2a - b, 2a - b - (c - b) = 2a - c, and 2a - c - (d - c) = 2a - d, so the correct result is (2a - a, 2a - b, 2a - c, 2a - d).
How it works
ḤḢ_ Main link. Argument: A (array)
Ḥ Unhalve; multiply all integers in A by 2.
Ḣ Head; extract first element of 2A.
_ Subtract the elements of A from the result.
Wonder, 17 bytes
@->#@- *2:0#1#0#0
Not sure why I didn't post this earlier. Usage:
(@->#@- *2:0#1#0#0)[5 6 7 8]
More readable:
@(map @
- (
* 2 get 0 #1
) #0
) #0
ised, 11 bytes
2*$1_0-$1
Invocation: ised --l 'file with input.txt' '2*$1_0-$1
(edit: corrected by stealing the algebra from Dennis)
Haskell, 20 19 bytes
f(x:r)=x:map(2*x-)r
Same solution as Dennis, thank you for your idea of 2a - x.
Saved one byte thanks to Christian Severs.
Dyalog APL, 5 bytes
this is a 5-train, it parses like two nested 3-trains ("forks"): -+(2×⊃)
reads like: the negation (-) of the whole array plus (+) twice (2×) the first element (⊃)
Labyrinth, 24 bytes
+:}:?
}
<}}?;%):,\!-{:{>
Input and output format are linefeed-separate lists (although the input format is actually a lot more flexible). The program terminates with an error.
I've got two other solutions at this byte count, which work basically the same but use somewhat different control flow.
:+:}:?
{
,}-!
? {
:";\@
{:+:}:?
_
<>__-?:;%):,\!
Explanation
The instruction pointer (IP) starts moving east along the first line, but all the commands before the ? are basically no-ops on the global state, since we're not using stack depth commands anywhere. So the code really starts at the ? going west, since the IP turns around when it hits the dead end.
The code therefore starts with the following linear bit of code:
?:}:+}
This simply sets us up with a copy of 2a to use the [2a - a, 2a - b, 2a - c, ...] formula.
? Read first integer a.
:} Move a copy off to the auxiliary stack.
:+ Multiply a by 2 (by adding it to itself).
} Move that off to the auxiliary stack as well.
We now enter the main loop of the program, using a fairly standard trick to loop through a single line of code:
<...>
Note that the stack will be empty whenever we hit the < so we know we'll get zeros there. The < then rotates the entire line left, taking the IP with it, so we get this:
...><
The IP then has to move left, where the > shifts the line back into its original place (to prepare it for the next iteration). Then the line is simply executed from right to left, so a single loop iteration is this:
{:{-!\,:)%;?}}
The catch when working with a loop of this type is that you can't work with any form of conditional execution, since Labyrinth doesn't have a way to skip code. Therefore, we will terminate the program with a division by zero when we hit EOF. Here is a breakdown of each loop iteration.
{: Pull 2a back to the main stack and make a copy.
{ Pull the latest value i of the input list back to main as well.
- Compute 2a-i/
!\ Print it with a trailing linefeed.
, Read a character. If there are input elements left, this will be some
form of separator character, and therefore a positive value x. However,
at the end of the program, this will give -1.
:) Make an incremented copy.
% Try to to compute x%(x+1). This will error out at EOF.
; Discard the result of the division.
? Read the next input value.
}} Move that and the remaining copy of 2a back to the auxiliary stack.
Labyrinth, 34 bytes
?:_2*}
@ _
)\?_1"
, ;
!`-}:{
Uses @Dennis's (2a - a, 2a - b, 2a - c, 2a - d) approach.
The yellow tiles are for control flow. In this 2D programming language, the program starts at the top-left-most tile moving east to start. At junctions, the direction is determined by the sign of the top of the main stack. Blank tiles are walls.
Green
This section saves 2a to the auxilary stack.
?Get the first number and push it to the top of the main stack:Duplicate the top of the stack_2Push two to the top of the stack*Popy, popx, pushx*y}Move the top of the main stack to the top of the auxilary stack._Push zero to the top of the stack
Orange
This section subtracts 2a from the current number, negates the result, outputs the result, gets the next character (the delimeter), exits if EOF, outputs a newline, gets the next number.
"Noop. If coming from the north, the top of the stack will be zero and the program will continue south. If coming from the west, the top of the stack will be one and the program will turn right (continuing south);Discard the top of the stack. As the zero or one is only used for control flow, we need to discard these{Move the top of the auxilary stack (2a) to the top of the main stack:Duplicate the top of the main stack}Move the top of the main stack to the top of the auxilary stack-Popy, popx, pushx-y\`` Negate the top of the stack. This and the previous three operations have the effect of-(x-2a) = 2a-x`!Pop the top of the stack and output it as a number,Push the next character (which will be the delimiter) or negative one if EOF)Increment the top of the stack. If the last character is EOF, then the top of the stack will now be zero and the program will continue straight to the@and exit. If the last character was a delimeter, then the top of the stack will be positive causing the program to turn right and continue east to the\\Output a newline?Get the next number_1Push one to the top of the stack in order to turn right at the junction
Perl, 26 + 3 (-pla flag) = 29 bytes
$_="@{[map$F[0]*2-$_,@F]}"
or
$_=join$",map$F[0]*2-$_,@F
Using:
perl -plae '$_="@{[map$F[0]*2-$_,@F]}"' <<< "32 18 25 192 199"
Python 2, 30 bytes
lambda x:[x[0]*2-n for n in x]
Test it on Ideone.
How it works
The deltas of (a, b, c, d) are b - a, c - b, and d - c. Cumulatively reducing (a, b - a, c - b, d - c) by subtractiong yields a - (b - a) = 2a - b, 2a - b - (c - b) = 2a - c, and 2a - c - (d - c) = 2a - d, so the correct result is (2a - a, 2a - b, 2a - c, 2a - d).
Mathematica, 8 bytes
2#-{##}&
Unnamed function taking an indeterminate number of arguments. This uses an "easy" way: negates the entire list and adds twice the (original) first element.
Called for example like 2#-{##}&[1,3,4,2,8]; returns a list like {1,-1,-2,0,-6}.
Pushy, 9 bytes
{&}2*K~-_
Give arguments as comma-seperated values on cmd line: $ pushy invdeltas.pshy 1,3,4,2,8. Here's the breakdown, with example stack:
% Implicit: Input on stack [1, 3, 4, 2, 8]
{&} % Copy first item, put at end of stack [1, 3, 4, 2, 8, 1]
2* % Multiply by 2 [1, 3, 4, 2, 8, 2]
K~ % Negate everything on stack [-1, -3, -4, -2, -8, -2]
- % Subtract last item from all [1, -1, -2, 0, -6]
_ % Print whole stack
Note: this can be 8 bytes if backwards output is allowed: @&2*K~-_
APL, 8 bytes
+\⊃,2-/+
Explanation:
+\ ⍝ running sum of
⊃ ⍝ first item of list
, ⍝ followed by
2-/ ⍝ differences between every pair in
+ ⍝ input list
Test cases:
( +\⊃,2-/+ ) ¨ (5 6 7 8) (1 3 4 2 8) (32 18 25 192 199)
┌───────┬────────────┬──────────────────┐
│5 4 3 2│1 ¯1 ¯2 0 ¯6│32 46 39 ¯128 ¯135│
└───────┴────────────┴──────────────────┘
PHP, 48 bytes
for(;''<$c=$argv[++$i];)echo-$c+2*$a=$a??$c,' ';
Using the technique from Dennis. Use like:
php -r "for(;''<$c=$argv[++$i];)echo-$c+2*$a=$a??$c,' ';" 1 3 4 2 8
Non-Dennis 55 byte version:
for(;''<$c=$argv[++$i];$l=$c)echo$a+=($l??$c*2)-$c,' ';
CJam, 16 bytes
Input format: [1 2 3 4]. Uses the easy formula.
l~_(2*/;a/,@@*.-
Explanation:
l~_(2*/;a/,@@*.-
l~_ e#Read input twice into an array. Stack: [1 2 3 4] [1 2 3 4]
( e#Get first element of the array. Stack: [1 2 3 4] [2 3 4] 1
2* e#Multiply by two. Stack: [1 2 3 4] [2 3 4] 2
/; e#Discard second element. Stack: [1 2 3 4] 2
a e#Wrap into an array. Stack: [1 2 3 4] [2]
/, e#Rotate and get length. Stack: [2] [1 2 3 4] 4
@@ e#Rotate twice. Stack: [1 2 3 4] 4 [2]
* e#Repeat len times. Stack: [1 2 3 4] [2 2 2 2]
.- e#Vectorized substraction. Stack: [-1 0 1 2]
e#Implictly print
Sorry for no test link. I guess SE don't like it's links with brackets inside.
C++14, 36 bytes
As unnamed lambda modifying its input:
[](auto&c){for(auto&x:c)x=2*c[0]-x;}
Using the technique from Dennis. Works for any container like int[] or vector<int>.
Usage:
#include<iostream>
auto f=
[](auto&c){for(auto&x:c)x=2*c[0]-x;}
;
int main(){
int a[] = {1, 3, 4, 2, 8};
f(a);
for(auto&x:a)
std::cout << x << ", ";
std::cout<<"\n";
}
Brain-Flak, 76 Bytes
([][()]){{}(({})<(({}){}[{}]<>)<>>)([][()])}{}({}<<>([]){{}({}<>)<>([])}<>>)
Explanation:
Part 1:
( ) # Push:
[] # the height of the stack
[()] # minus 1
{ } # While the height - 1 != 0:
{} # Pop the height
(({})< # Hold onto the top value, but put it back.
# This ensures that the top value is always
# what was the first element of input
( ) # Push:
({}){} # Top * 2
[{}] # minus the next element
<> <> # onto the other stack
>) # Put back the element we held onto.
( ) # Push:
[] # The height of the stack
[()] # Minus 1
{} # Pop the counter used for the height
Part 2:
({}< # Hold onto the top element.
# This was the first number for input
# so it needs to end up on top
<> # Switch stacks
([]) # Push the height of the stack
{ } # While the height != 0:
{} # Pop the height
( ) # Push:
{} # The top element of this stack
<> # onto the other stack
<> # and switch back
([]) # Push the new height of the stack
<> # After every element has switched stacks
# (which reverses their order),
# switch stacks
>) # Push the first element back on
Perl 6, 40 16 bytes
{[\+] .[0],|.rotor(2=>-1).map({[-] @_})}
{.map(.[0]*2-*)}
Expanded:
{ # bare block lambda with single implicit parameter 「$_」 ( input is a List )
[\[+]] # triangle reduce the following using 「&infix:<+>」
.[0], # the first value
|( # Slip this list into outer one ( Perl 6 doesn't auto flatten )
.rotor( 2 => -1 ) # take the input 2 at a time, backing up 1
.map({ [-] @_ }) # reduce the pairs using 「&infix:<->」
)
}
{ # bare block lambda with single implicit parameter 「$_」 ( input is a List )
.map( # map over the inputs
.[0] * 2 - * # take the first value multiply by 2 and subtract the current value
# ^- this makes the statement a WhateverCode, and is the input
)
}
Ruby, 23 bytes
->l{l.map{|x|l[0]*2-x}}
Not particularly original.
05AB1E, 4 bytes
¬·s-
Try it online! or as a Test suite
Explanation
¬ # head of input list
· # multiplied by 2
s # swap with input list
- # subtract
JavaScript (ES6), 21
Thx @Dennis
l=>l.map(v=>l[0]*2-v)
R, 23 18 17 bytes
x=scan();2*x[1]-x
auto-vectorisation and default print to the rescue!
Python, 44 bytes
lambda l:[l[0]]+[x-(x-l[0])*2for x in l[1:]]
This uses the "Easier method".
