The idea of this is mainly from BIO 2017 q1. I got the idea for posting this challenge from my Binary Sequences challenge, since lots of people seemed to like it.

Also, this is the first challenge I've posted without posting on the sandbox. I'll delete it if no one likes it.


Take in a sequence of digits in ternary (base 3); this could be as a string, an array or the numerical value along with the number of preceding zeros.

For each row in the triangle, a row below is generated until there is only one digit in the last row. To find a digit below two other digits, the digit will be the same as two above it if these two other digits above are equal. Otherwise, it will be the digit that is not equal to either of them. Here is an example:

0 0 1 2 0 1 2 2
 0 2 0 1 2 0 2
  1 1 2 0 1 1
   1 0 1 2 1
    2 2 0 0
     2 1 0
      0 2

You are only expected to return the last row.

Make your code short.

Test Cases

0 -> 0
11 -> 1
10 -> 2
000 -> 0
012 -> 1
21102 -> 2
201021 -> 1
111111 -> 1
1020202020 -> 2
0212121210 -> 0

12 Answers 12


Husk, 9 bytes


Try it online!


The main idea is to compute the mapping of two digits to one as f(a,b) = (-a-b) % 3. For golfing purposes we can delay the modulo until the very end.

   Ωε       Apply the following function until the list is only one
            element in length.
     Ẋo       Apply the following function to pairs of adjacent values.
       _+       Add the two values and negate the result.
  ←         Take the first (and only) element of this list.
%3          Take it modulo 3.

In principle, it's also possible to compute the result directly by multiplying each element by the corresponding binomial coefficient and multiplying the sum by -1 for even-length lists, but I don't know of a way to do that in fewer bytes.


MATL, 10 bytes


Try it online! Or verify all test cases.


For each pair of digits, the code computes twice the sum modulo 3. The process is repeated as many times as the length of the input minus 1.

t        % Implicit input: array of length n. Duplicate
d        % Consecutive differences. Gives an array of length n-1
"        % For each (that is, do n-1 times)
  HYC    %   2-column matrix where each column is a sliding block of length 2
  E      %   Times 2, element-wise
  s      %   Sum of each column
  I\     %   Modulo 3
         % Implicit end. Implicit display

Python 2, 48 bytes

f=lambda a,*l:-(f(*l)+f(a,*l[:-1]))%3if l else a

Try it online!

Recurses on the sublists deleting the first and last elements respectively.

This would be cleaner in Python 3 if it could actually unpack f=lambda a,*b,c:....


Emojicode, 242 bytes

🐋🍨🍇🐖🔢➡️🚂🍇🔂i⏩➖🐔🐕1 0🍇🔂j⏩0i🍇🍊❎😛🍺🔲🐽🐕j🚂🍺🔲🐽🐕➕1j🚂🍇🐷🐕j➖➖3🍺🔲🐽🐕j🚂🍺🔲🐽🐕➕1j🚂🍉🍉🍉🍎🍺🔲🐽🐕0🚂🍉🍉

Uses the same algorithm as my C answer. Try it online!


Jelly, 9 bytes


Try it online!

Using Martin Ender's Husk algorithm.


Haskell, 36 bytes

f(h:t)=mod(-f t-f(h:init t))3

Try it online!

Saves 1 byte over the more symmetrical:

f l=mod(-f(tail l)-f(init l))3

Try it online!

The idea is simple: recursively compute the function on the sublists deleting the first and last element respectively, and combine them with \a b -> mod(-a-b)3. This seems shorter than zipWith'ing this fuction .

Haskell, 44 bytes

f[a]=mod a 3
f l=f$zipWith((-).(0-))l$tail l

Try it online!


C (gcc), 91 88 84 bytes

-1 byte thanks to @Mr.Xcoder!


Gets the array and the length. Try it online!


J, 23 15 Bytes


Thanks to @miles

Old Solution:


Inspired by Martin Ender's solution:


3|2&(-@+/\)^:(#>1:)^:_]    | Whole program
                      ]    | Seperates the argument from the _ (infinity)
           ^:(#>1:)^:_     | Do while the length is greater than one
  2&(-@+/\)                | Inverse of the sum of adjacent elements
3|                         | Modulo 3
  • 2
    \$\begingroup\$ 15 bytes with 3&(|2+/\-)~<:@# \$\endgroup\$
    – miles
    Dec 30, 2017 at 20:46
  • \$\begingroup\$ @miles, ha, i was just about to post this for 19 bytes 3|((2<.#)-@+/\])^:_ -- yours is really nice. \$\endgroup\$
    – Jonah
    Dec 30, 2017 at 21:32

APL (Dyalog), 17 bytes


Try it online!


2+/⍵ - sum each two adjacent items

3- - vectorized subtract from three

3| - vectorized modulo by three

- repeat until...

1=≢⍺ - only one item is left


Javascript (ES6), 58 bytes


Batch, 122 bytes

@for %%e in (%*)do @set/al+=1,n^^=3
@for %%e in (%*)do @set/as+=%%e*n,s%%=3,n*=l-=1,n/=i+=1
@echo %s%

Uses binomial expansion. As @MartinEnder points out, the sum has to be negated (modulo 3) if the number of values (which are counted in the first loop) is even, so n is set to either 1 or 2 accordingly. The second loop then computes the sum via the binomial coefficients.


APL+WIN, 30 28 bytes

2 bytes saved courtesy of Uriel.



n←⎕ Prompt for screen input of the form: 0 0 1 2 0 1 2 2

'n←3|3-2+/n' Successive rows are 3 mod 3 minus successive digit pairs.

(⍴n)⍴⊂ Create a nested vector of the row code, one element per row. 

¯1↑∊⍎¨ Execute each element of row code, flatten result and take final value.

This is one way of writing looping code in APL on a single line.

  • \$\begingroup\$ You don't need the rightmost 3| \$\endgroup\$
    – Uriel
    Dec 30, 2017 at 17:55
  • \$\begingroup\$ @Uriel. Thanks. \$\endgroup\$
    – Graham
    Dec 30, 2017 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.