| Bytes | Lang | Time | Link |
|---|---|---|---|
| 005 | Japt x | 180326T233802Z | Shaggy |
| 033 | Python | 180329T033534Z | xnor |
| 009 | APL Dyalog | 180326T223603Z | Uriel |
| 015 | x86 | 180327T001823Z | qwr |
| 009 | Wolfram Language Mathematica | 180327T044713Z | alephalp |
| 028 | Java 8 | 180327T091232Z | Kevin Cr |
| 005 | 05AB1E | 180326T224450Z | Uriel |
| 012 | J | 180327T071358Z | Galen Iv |
| 008 | Stax | 180327T061533Z | Weijun Z |
| 038 | JavaScript ES6 | 180326T223610Z | Arnauld |
| 020 | Octave | 180327T012624Z | Nick Alg |
| 028 | Ruby | 180327T010156Z | Level Ri |
| 032 | Python 2 | 180326T231007Z | Dennis |
| 044 | SHELL | 180326T231544Z | Ali ISSA |
| 013 | Add++ | 180327T001446Z | caird co |
| 007 | Husk | 180326T235940Z | Sophia L |
| 026 | Haskell | 180326T232136Z | totallyh |
| 005 | Jelly | 180326T223142Z | Dennis |
| 056 | Ruby | 180326T224454Z | benj2240 |
| 008 | Jelly | 180326T221801Z | Erik the |
| 016 | CJam | 180326T221338Z | Peter Ta |
Japt -x, 7 5 bytes
ä!gUÌ
ä!gUÌ :Implicit input of array U
ä :Consecutive pairs reduced by
!g : Signs of differences, inverted
UÌ :After first prepending last element of U
:Implicit output of sum of resulting array
Python, 33 bytes
lambda i,j,k:(i^j!=k or-~j-i)%3-1
I tried for a while to beat the product-of-differences approach, but the best I got was 1 byte longer.
x86, 15 bytes
Takes arguments in %al, %dl, %bl, returns in %al. Straightforward implementation using Dennis's formula.
6: 88 c1 mov %al,%cl
8: 28 d0 sub %dl,%al
a: 28 da sub %bl,%dl
c: 28 cb sub %cl,%bl
e: f6 e3 mul %bl
10: f6 e2 mul %dl
12: d0 f8 sar %al
14: c3 retq
Aside: I think I understand why %eax is the "accumulator" now...
Wolfram Language (Mathematica), 9 bytes
Signature
Wolfram Language (Mathematica), 18 bytes
Saved 2 bytes thanks to Martin Ender.
Det@{#^0,#,#^2}/2&
J, 12 bytes
1#.2*@-/\4$]
Direct translation of Uriel's APL solution into J.
Explanation:
4$] Extends the list with its first item
2 /\ do the following for all the overlapping pairs in the list:
*@- find the sign of their difference
1#. add up
JavaScript (ES6), 38 bytes
Overcomplicated but fun:
(a,b,c,k=(a+b*7+c*13)%18)=>k-12?+!k:-1
JavaScript (ES6), 28 bytes
Using the standard formula:
(a,b,c)=>(a-b)*(b-c)*(c-a)/2
Octave, 20 bytes
@(v)det(eye(3)(:,v))
Pretty direct implementation of the determinant formula. Permutes the columns of the identity matrix then takes the determinant.
Python 2, 32 bytes
lambda i,j,k:(i-j)*(j-k)*(k-i)/2
Algorithm
Let's consider the differences i-j, j-k, k-i.
If (i, j, k) is a rotation of (1, 2, 3), the differences are a rotation of (-1, -1, 2). Taking the product, we get (-1) × (-1) × 2 = 2.
If (i, j, k) is a rotation of (3, 2, 1), the differences are a rotation of (1, 1, -2). Taking the product, we get 1 × 1 × (-2) = -2.
For (i, i, j) (or a rotation), where i and j may be equal, the differences are (0, i-j, j-i). Taking the product, we get 0 × (i-j) × (j-i) = 0.
Thus, dividing the product of the differences by 2 yields the desired result.
SHELL, 44 Bytes
F(){ bc<<<\($2-$1\)*\($3-$1\)*\($3-$2\)/2;}
tests :
F 1 2 3
1
F 1 1 2
0
F 2 3 1
1
F 3 1 2
1
F 3 2 1
-1
F 2 1 3
-1
F 1 3 2
-1
F 1 3 1
0
Explanation :
The formula is : ((j - i)*(k - i)*(k - j))/2
BC, 42 Bytes
define f(i,j,k){return(j-i)*(k-i)*(k-j)/2}
tests:
f(3,2,1)
-1
f(1,2,3)
1
f(1,2,1)
0
Husk, 7 bytes
ṁ±Ẋ-S:←
Explanation
Straight port of Dennis's Jelly answer. S:← copies the head of the list to the end, Ẋ- takes adjacent differences, and ṁ± takes the sign of each element and sums the result.
Jelly, 5 bytes
ṁ4IṠS
Algorithm
Let's consider the differences j-i, k-j, i-k.
If (i, j, k) is a rotation of (1, 2, 3), the differences are a rotation of (1, 1, -2). Taking the sum of the signs, we get 1 + 1 + (-1) = 1.
If (i, j, k) is a rotation of (3, 2, 1), the differences are a rotation of (-1, -1, 2). Taking the sum of the signs, we get (-1) + (-1) + 1 = -1.
For (i, i, j) (or a rotation), where i and j may be equal, the differences are (0, j-i, i-j). The signs of j-i and i-j are opposite, so the sum of the signs is 0 + 0 = 0.
Code
ṁ4IṠS Main link. Argument: [i, j, k]
ṁ4 Mold 4; yield [i, j, k, i].
I Increments; yield [j-i, k-j, i-k].
Ṡ Take the signs, replacing 2 and -2 with 1 and -1 (resp.).
S Take the sum.
Ruby, 56 bytes
->t{t.uniq!? 0:(0..2).any?{|r|t.sort==t.rotate(r)}?1:-1}
Once we rule out cases where the values of the triplet are not unique, t.sort is equivalent to (and shorter than) [1,2,3] or [*1..3]
->t{
t.uniq! ? 0 # If applying uniq modifies the input, return 0
: (0..2).any?{|r| # Check r from 0 to 2:
t.sort==t.rotate(r) # If rotating the input r times gives [1,2,3],
} ? 1 # return 1;
:-1 # else return -1
}
CJam (16 bytes)
1q~{)1$f-@+:*\}h
Online demo. Note that this is based on a previous answer of mine which uses the Levi-Civita symbol to calculate the Jacobi symbol.