# Ternary Triangles

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.

### Rules

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
1


You are only expected to return the last row.

### Test Cases

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


## Husk, 9 bytes

%3←ΩεẊo_+


Try it online!

### Explanation

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

td"HYCEsI\


### Explanation

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!

+2\N$Ḋ¿%3  Try it online! Using Martin Ender's Husk algorithm. # Haskell, 36 bytes f[a]=a f(h:t)=mod(-f t-f(h:init t))3  Try it online! Saves 1 byte over the more symmetrical: f[a]=a 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), 9188 84 bytes

-1 byte thanks to @Mr.Xcoder!

j;f(a,l)int*a;{for(;l-->1;)for(j=0;j<l;)a[j++]=a[j]^a[j+1]?3-a[j]-a[j+1]:a[j];a=*a;}


Gets the array and the length. Try it online!

# J, 23 15 Bytes

3&(|2+/\-)~<:@#


Thanks to @miles

### Old Solution:

3|2&(-@+/\)^:(#>1:)^:_]


Inspired by Martin Ender's solution:

### Explanation

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

• 15 bytes with 3&(|2+/\-)~<:@# – miles Dec 30 '17 at 20:46
• @miles, ha, i was just about to post this for 19 bytes 3|((2<.#)-@+/\])^:_ -- yours is really nice. – Jonah Dec 30 '17 at 21:32

## Batch, 122 bytes

@set/an=2,s=i=l=0
@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 (Dyalog), 17 bytes

{3|3-2+/⍵}⍣{1=≢⍺}


Try it online!

How?

2+/⍵ - sum each two adjacent items

3- - vectorized subtract from three

3| - vectorized modulo by three

⍣ - repeat until...

1=≢⍺ - only one item is left

# APL+WIN, 30 28 bytes

2 bytes saved courtesy of Uriel.

n←⎕⋄¯1↑∊⍎¨(⍴n)⍴⊂'n←3|3-2+/n'


Explanation:

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.

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

# Javascript (ES6), 58 bytes

f=s=>s[1]?f(s.replace(/.(?=(.?))/g,(a,b)=>b&&(6-a-b)%3)):s