| Bytes | Lang | Time | Link |
|---|---|---|---|
| 014 | APLDyalog Unicode | 250509T154747Z | Mat_rdv |
| 054 | Janet | 250508T180519Z | Adam |
| 048 | Juby | 250508T143655Z | Jordan |
| 011 | Pyth | 170726T221103Z | Anders K |
| 006 | Japt | 170723T203134Z | Justin M |
| 091 | Java | 170725T090249Z | RCB |
| 053 | Haskell | 170724T162506Z | nimi |
| 013 | Brachylog | 170724T141221Z | Erik the |
| 006 | 05AB1E | 170724T140049Z | Erik the |
| 058 | C++14 | 170723T215313Z | vaultah |
| 056 | Python 3 | 170723T215106Z | ovs |
| 025 | Mathematica | 170724T084307Z | user6198 |
| 013 | CJam | 170724T081029Z | Peter Ta |
| 065 | Cubix | 170724T024159Z | MickyT |
| 032 | R | 170723T230700Z | MickyT |
| 051 | PHP | 170723T223234Z | Titus |
| 012 | Pyth | 170723T220150Z | notjagan |
| 060 | Mathematica | 170723T212502Z | ZaMoC |
| 038 | JavaScript ES6 | 170723T210113Z | Neil |
| 072 | Python | 170723T202041Z | Uriel |
| 004 | Jelly | 170723T204759Z | Leaky Nu |
| 003 | Husk | 170723T204048Z | Zgarb |
| 006 | MATL | 170723T202328Z | Luis Men |
APL(Dyalog Unicode), 14 bytes SBCS
+/2⊥¨⊢⊂⍨1,2≠/,
1 and ¯1 are used as values.
Explanation
, ravel (vectors stay the same, scalars turn into 1-element vector)
/ reduce
≠ not equal
2≠/, 2-wise reduce: where the sequence values change
, concatenate
1, concatenate 1 with...
⍨ reverse order of arguments
⊂ partition
⊢ the values
⊢⊂⍨1,2≠/, splitting the sequence into parts with identical values
¨ each
⊥ decode
2⊥ decode from binary (same as 1+2+4+8+... for positive parts and -1-2-4-8-... for negative parts)
/ reduce
+/ sum
+/2⊥¨⊢⊂⍨1,2≠/, solution
Janet, 55 54 bytes
|(+;(do(var a 0)(map|(+= a(if(>(* a $)0)a(- $ a)))$)))
J-uby, 48 bytes
Same technique as Anders and Luis.
:slice_when+:!=|:sum+:*%[:first,:+@|:**&2|~:-&1]
Explanation
:slice_when + :!= | :sum + :* % [:first, :+@ | :** & 2 | ~:- & 1]
:slice_when + :!= | # Slice array into runs of equal elements, then
:sum + :* % [ , ] # Sum products of
:first # The first element of each run and
:+@ | :** & 2 | ~:- & 1 # 2**n-1, where n is the size of the run
Pyth, 11 bytes
s.b*t^2NYr8
How it works
r8 run-length encode input
.b map (N, Y) ↦
^2N 2^N
t minus 1
* Y times Y
s sum
Japt, 8 6 bytes
-2 bytes thanks to @ETHproductions
ò¦ xì2
Explanation
Implicit input: [1, 1, 1, -1, -1, -1, -1, 1, 1]
ò¦
Partition input array (ò) between different (¦) elements:
[[1, 1, 1], [-1, -1, -1, -1], [1, 1]]
ì2
Map each partition to itself parsed as an array of base-2 digits (ì): [7, -15, 3]
x
Get the sum (x) of the resulting array: -5
Java, 91 bytes
int f(int[]a){int s=0,r=0,i=-1;while(++i<a.length)r+=s=s!=0&s>0==a[i]>0?2*s:a[i];return r;}
Haskell, 54 53 bytes
k#(a:b:c)=k+last(b:[k*2|a==b])#(b:c)
k#_=k
(#)=<<head
A simple recursion that either doubles the accumulator k or resets it to 1/-1 and adds the values of each step.
Brachylog, 13 bytes
ḅ⟨{ȧᵐ~ḃ}×h⟩ᵐ+
Brachylog uses _ instead of -.
Explanation:
?ḅ⟨{ȧᵐ~ḃ}×h⟩ᵐ+. Predicate (implicit ?.)
? The input
ḅ A list where its elements' elements are equal, and when concatenated is ?
ᵐ The result of mapping this predicate over ?
⟨ ⟩ The result of forking two predicates over ? with a third
{ } The result of this predicate on ?
ᵐ The result of mapping this predicate over ?
ȧ The absolute value of ?
~ An input where the result of this predicate over it is ?
ḃ A list that represents the digits of ? in base I (default 2)
h An object that is the first element of ?
× A number that is the product of ?
+ A number that is the sum of ?
. The output
C++14, 58 bytes
[](auto v,int&s){int p=s=0;for(auto x:v)s+=p=x*p<1?x:2*p;}
Takes input via the v argument (std::vector, or any iterable container), outputs to the s argument (by reference). Each element of v must be either 1 or -1.
Python 3, 57 56 bytes
-1 byte thanks to @notjagan
f=lambda a,*s,m=1:m*a+(s>()and f(*s,m=(m*2)**(a==s[0])))
Mathematica, 25 bytes
Tr[Fold[#+##&]/@Split@#]&
CJam (13 bytes)
{e`{(*2b}%1b}
Online test suite. This is an anonymous block (function) which takes an array of ints on the stack and leaves an int on the stack. The last test shows that it handles an empty array correctly, giving 0.
The approach is straightforward run-length encoding followed by a manual run-length decode of each run and base conversion. Using the built-in for run-length decode I get one byte more with {e`1/:e~2fb1b} or {e`{ae~2b}%1b}.
Cubix, 65 bytes
W(?\q.p)w.;0.w;/0>I!U-unP(nwUs;q\^q:;^!u?P(w!u+w.;;>2p!u/@Os..sr\
W ( ? \
q . p )
w . ; 0
. w ; /
0 > I ! U - u n P ( n w U s ; q
\ ^ q : ; ^ ! u ? P ( w ! u + w
. ; ; > 2 p ! u / @ O s . . s r
\ . . . . . . . . . . . . . . .
. . . .
. . . .
. . . .
. . . .
As a brief explanation of this:
- Read in each integer (1 or -1) and compare it to previous. If:
- the same push it to the bottom as the start of a counter
- else bring counter to top and increment/decrement it as appropriate.
- Once input is finished bring each counter to the top and handling negatives do 2 ^ counter - 1
- Sum the results and output
R, 32 bytes
sum((2^(R=rle(scan()))$l-1)*R$v)
This is the same method as a few others here.
With the input of -1 -1 1 1 -1 -1 -1
- Do a Run Length Encoding on the input. Results with lengths of
2, 2, 3and values-1, 1, -1 - Do 2 to power of lengths - 1. Results in
3, 3, 7 - Multiply by the RLE values giving
-3, 3, -7 - Return the sum
-7
Mathematica, 60 bytes
Tr[Last@*Accumulate/@(#[[1]]2^(Range@Tr[1^#]-1)&/@Split@#)]&
JavaScript (ES6), 38 bytes
a=>a.map(e=>r+=d=d*e>0?d+d:e,r=d=0)&&r
Python, 76 72 bytes
f=lambda s,k=1:len(s)and(f(s,s[0])if s[0]*abs(k)/k-1else k+f(s[1:],2*k))
Husk, 3 bytes
ṁḋg
Explanation
ṁḋg
g Group equal adjacent elements,
ṁ take sum of
ḋ base-2 decoding of each group.
MATL, 6 bytes
Y'Wq*s
Try it online! Or verify all test cases.
Explanation
Consider input [1 1 1 1 1 1 -1 -1 -1 -1 1 1 1].
% Implicit input
% STACK: [1 1 1 1 1 1 -1 -1 -1 -1 1 1 1]
Y' % Run-length encoding
% STACK: [1 -1 1], [6 4 3]
W % Exponentiation with base 2, element-wise
% STACK: [1 -1 1], [64 16 8]
q % Subtract 1
% STACK: [1 -1 1], [63 15 7]
* % Multiply, element-wise
% STACK: [63 -15 7]
s % sum of array
% STACK: 55
% Implicit display