22
\$\begingroup\$

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.

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
\$\endgroup\$

12 Answers 12

9
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

MATL, 10 bytes

td"HYCEsI\

Try it online! Or verify all test cases.

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
\$\endgroup\$
3
\$\begingroup\$

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:....

\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

Jelly, 9 bytes

+2\N$Ḋ¿%3

Try it online!

Using Martin Ender's Husk algorithm.

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 91 88 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!

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
  • 2
    \$\begingroup\$ 15 bytes with 3&(|2+/\-)~<:@# \$\endgroup\$ – miles Dec 30 '17 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 '17 at 21:32
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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.

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

Javascript (ES6), 58 bytes

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

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.