Determine dice value from side view Write program or function which will take left and right value of dice as integers (1-6) and return value on top.

Dice layout:

    +---+
    | 1 |
+---+---+---+---+
| 2 | 3 | 5 | 4 |
+---+---+---+---+
    | 6 |
    +---+

  ,^.
&lt;´ 5 `&gt;  &lt;-- Top value
|`._,´|
.6 | 4,  &lt;-- Side values
 `.|,´


So inputting 6 4 will return 5.

Order is important:

2 3 -&gt; 1
3 2 -&gt; 6


Program doesn't have to work with invalid input values.

To discourage obvious approach (using table with all combinations), using any builtin text encoding or de/compression routines or base encoding or anything else similar to reduce size is not allowed. Note that using table is still allowed and rolling your own decompression is allowed too, as long as it's not done using some ready library function.

For reference purposes, here's a table of all combinations (i.e. all possible inputs and outputs):

23, 35, 42, 54 -&gt; 1
14, 31, 46, 63 -&gt; 2
12, 26, 51, 65 -&gt; 3
15, 21, 56, 62 -&gt; 4
13, 36, 41, 64 -&gt; 5
24, 32, 45, 53 -&gt; 6


Shortest code wins, and standard loopholes apply.
There's a nice polynomial expression modulo 7 for the third side given two sides a and b.

$${3(a^3b - ab^3) \mod 7}$$

or factored

$${3ab(a^2-b^2) \mod 7}$$

The modulo 7 maps to a remainder in {0,1,2,3,4,5,6}.

I explain why it works in this Math SE answer, though I do think there probably is a cleaner argument I'm missing. The only other two-term polynomial that works is

$${(3a^5b^5 - a^3b) \mod 7}$$

which I originally found by transforming my bit-bashing into arithmetic operations, then did a brute-force search over polynomials of this form to find the nicer one.

Please feel free to add ports of this into your favorite language; this is a CW post.

J, 9 by Synthetica

7|3***+*-


See my post

Dyalog APL, 9 by ngn (typo fixed by Adám)

7|3×××+×-


Blatantly stolen from above J answer.

TI-Basic, 14 by Timtech

7fPart((A³B-AB³)/21


Pyth, 16 by FryAmTheEggman

M%*3-*H^G3*^H3G7


Defines a function g of two values.

Golfscript, 18 by Peter Taylor (old polynomial)

~1$*.5?3*@.*@*- 7%


CJam, 18 by Martin Büttner (ported from Peter's GolfScript) (old polynomial)

l~1$*_5#3*@_*@*m7%


Mathematica, 20 by Martin Büttner

Mod[+##(#-#2)3##,7]&amp;


Yes, that's a unary plus, and no, there's no shorter way that doesn't use a unary plus.

dc, 21 by Toby Speight

sb7+d3^lb*rlb3^*-3*7%


I have to add 7 to a to ensure the difference is always positive (dc has a signed % operator).

Julia, 24 23 by Martin Büttner

f(a,b)=3a*b*(a^2-b^2)%7


CoffeeScript, 28 26 by rink.attendant.6

x=(a,b)-&gt;3*a*b*(a*a-b*b)%7


JavaScript (ES6), 28 26 by rink.attendant.6

x=(a,b)=&gt;3*a*b*(a*a-b*b)%7


Essentially the same as CoffeeScript.

Python 28, by xnor

lambda a,b:3*a*b*(a*a-b*b)%7


Bash, 31

Nothing special:

echo $[3*($1**3*$2-$1*$2**3)%7]


or alternatively:

echo $[3*$1*$2*($1*$1-$2*$2)%7]


Another (longer but perhaps interesting) approach.

Nim, 36 by Sillesta

proc(x,y:int):int=3*x*y*(x*x-y*y)%%7


Java 7, 46 44 by rink.attendant.6

int f(int a,int b){return(a*a-b*b)*a*b*3%7;}


Java 8, 25 23 by Kevin Cruijssen

a-&gt;b-&gt;(a*a-b*b)*a*b*3%7


PHP, 49 47 by rink.attendant.6

function x($a,$b){echo($a*$a-$b*$b)*3*$a*$b%7;}


Batch, 52 unclemeat

set/aa=(3*(%1*%1*%1*%2-%1*%2*%2*%2)%%7+7)%%7
echo %a%


CMD does not support true modulus natively (so can't handle negative numbers) - hence %%7+7)%%7.

LESS (as a parametric mixin), 62 60 by rink.attendant.6

.x(@a,@b){@r:mod(3*@a*@b*(@a*@a-@b*@b),7);content:~"'@{r}'"}


See my post below.

05AB1E, 10 8 by Emigna (-2 bytes by Kevin Cruijssen)

nÆs`3P7%


Try it online.

Haskell, 31 27 25 by Generic Display Name



a#b=3*a*b*(a*a-b*b)`mod`7


Try it online!

Excel, 27 by by Wernisch

=MOD(3*(A1^3*B1-A1*B1^3),7)


Excel VBA, 25 by Taylor Scott


?3*[A1^3*B1-A1*B1^3]Mod 7


Forth (gforth) 41 by reffu



: f 2&gt;r 2r@ * 2r@ + 2r&gt; - 3 * * * 7 mod ;


Try it online!

C#, 23 by Kevin Cruijssen



a=&gt;b=&gt;(a*a-b*b)*a*b*3%7