21
\$\begingroup\$

This challenge takes place on the snub square tiling.

Start by choosing any triangle, and color it \$c_1\$. Next, find all tiles which touch this triangle at any vertex, and color them \$c_2\$. Next, find all tiles which share a vertex with any \$c_2\$-colored tile, and color these \$c_3\$. Continue this process ad infinitum.

Illustration

Concentric tiling on snub square tiling

Initial terms

The sequence begins

  a(1) = 1
  a(2) = 9
  a(3) = 21
  a(4) = 35

Notice:

  • a(1) = 1 corresponds to the red triangle;
  • a(2) = 9 corresponds to the number of tiles in the second, orange layer;
  • a(3) = 21 corresponds to the number of tiles in the third, green layer; and so on.

(Note, this sequence is not in the OEIS, but OEIS sequence A296368 is closely related.)

Challenge

Your goal is to write a program that takes in a positive integer n and returns the number of tiles that are colored \$c_n\$, (i.e. the number of tiles in the \$n\$-th layer.) This is a challenge, so the shortest code in bytes wins.

\$\endgroup\$
  • 3
    \$\begingroup\$ This looks like a lion! +1 \$\endgroup\$ – RGS Feb 4 at 21:42
  • 3
    \$\begingroup\$ Can this be zero indexed? \$\endgroup\$ – Jo King Feb 4 at 22:23
  • 1
    \$\begingroup\$ @JoKing, I'd like to keep it one-indexed—partly because folks have already submitted solutions with that assumption. \$\endgroup\$ – Peter Kagey Feb 4 at 22:26

17 Answers 17

7
\$\begingroup\$

Jelly, 8 bytes

’3cạ×ʋ12

Try it online!

How?

’3cạ×ʋ12 - Link: integer, n
’        - decrement              (n-1)                1   2   3   4   5   6   7  ...
      12 - twelve                 12                  12  12  12  12  12  12 12  ...
     ʋ   - dyad:
 3       -   three                3                    3   3   3   3   3   3   3  ...
  c      -   choose               3C(n-1)              1   3   3   1   0   0   0  ...
    ×    -   multiply             (n-1)*12             0  12  24  36  48  60  72  ...
   ạ     -   absolute difference  |3C(n-1)-(n-1)*12|   1   9  21  35  48  60  72  ...
\$\endgroup\$
  • \$\begingroup\$ Ah, cool that Jelly has a way to avoid duplicating . I still have no idea how any of it works =p \$\endgroup\$ – Grimmy Feb 4 at 23:34
  • \$\begingroup\$ Yeah even with Lynn's excellent tutorial and its Chains section, it's still a tricky beast :) \$\endgroup\$ – Jonathan Allan Feb 4 at 23:43
26
\$\begingroup\$

Ruby, 26 bytes

->n{~-n*12-496/4**n%4+1/n}

Try it online!

Revised version adding 1/n and subtracting 496/4**n%4 to get the +1,-3,-3,-1 offset for the first 4 terms.

Ruby, 32 bytes

->n{n>4?~-n*12:[0,1,9,21,35][n]}

Try it online!

After 4, the sequence settles down to (n-1)*12. See diagram below (the equilateral triangles have been distorted into 45 degree isosceles triangles and the entire diagram rotated 45 degrees, but it remains topologically equivalent.)

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Really nice job :D \$\endgroup\$ – RGS Feb 4 at 23:24
  • \$\begingroup\$ Well done on finding a way to simplify the topology and providing a closed formula for n>4! \$\endgroup\$ – G0BLiN Feb 5 at 17:52
9
\$\begingroup\$

JavaScript (ES6), 23 bytes

Based on Level River St's answer.

n=>[1,5,13,7][--n]^n*12

Try it online!

How?

We compute \$(n-1)\times12\$ and adjust the first 4 values with a XOR.

$$\begin{array}{c|c} n&1&2&3&4&5&6&7&8&9&10\\ \hline (n-1)\times12&0&12&24&36&48&60&72&84&96&108\\ \hline \text{XOR}&1&5&13&7&\color{grey}0&\color{grey}0&\color{grey}0&\color{grey}0&\color{grey}0&\color{grey}0\\ \hline a(n)&1&9&21&35&48&60&72&84&96&108 \end{array}$$

\$\endgroup\$
9
\$\begingroup\$

05AB1E, 9 bytes

<©12*3®cα

Try it online! or try a test suite.

<           # input - 1
 ©          # save to register
  12*       # multiply by 12
      ®     # push the register
     3 c    # binomial coefficient(3, input - 1)
        α   # absolute difference

With 0-indexing, this would be 7 bytes:

12*3Icα
\$\endgroup\$
7
\$\begingroup\$

Python, 31 bytes

lambda n:n*12-11-(n>4or 5%-n%5)

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Jelly, 15 9 bytes

’3cạ×12$Ʋ

Try it online!

A monadic link taking \$n\$ as its argument and returning \$a(n)\$.

Based on @LevelRiverSt’s clever Ruby answer so be sure to upvote that one too!

Thanks to @Grimmy for saving 6 bytes!

Explanation

 ’       | Subtract 1
       Ʋ | Following as a monad
3c       | - Number of ways of picking (n-1) items from 3
  ạ   $  | - Absolute difference from:
   ×12   |   - Multiply (n-1) × 12
\$\endgroup\$
  • 1
    \$\begingroup\$ Here’s 9 bytes \$\endgroup\$ – Grimmy Feb 4 at 23:17
  • 1
    \$\begingroup\$ @Grimmy thanks! I’d missed that because of the difference for n=1 being -1, though of course with absolute difference that doesn’t matter. \$\endgroup\$ – Nick Kennedy Feb 4 at 23:24
  • \$\begingroup\$ @Grimmy I was trying to golf exactly that, and now managed it :) \$\endgroup\$ – Jonathan Allan Feb 4 at 23:31
4
\$\begingroup\$

APL (Dyalog Unicode), 14 bytesSBCS

12(|×-3!⍨⊢)-∘1

Try it online!

Direct translation of Jonathan Allan's Jelly answer. Even the code structure is the same. Jelly is a golfy descendant of APL; if you want to learn Jelly, learn APL first!

How it works

12(|×-3!⍨⊢)-∘1  ⍝ Monadic train, input: n
12(       )-∘1  ⍝ Pass on to the inner function with left←12 and right←n-1
    ×             ⍝ left × right
     -            ⍝ minus
      3!⍨⊢        ⍝ binomial(3, right)
   |              ⍝ absolute value of the above
\$\endgroup\$
4
\$\begingroup\$

brainfuck, 50 bytes

>>>>>>+<++<<----<+<,-[>++++++++++++[[>+<-]<]>>-]>.

Does i/o as raw byte values, like the others here.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can remove leading >> by using the implementation available on TIO. \$\endgroup\$ – Bubbler Feb 6 at 1:46
  • 2
    \$\begingroup\$ That's a nonstandard language extension that I'd like to see stamped out. I'm not a fan of the fragmentation of brainfuck into incompatible dialects, and I work against that where I can. \$\endgroup\$ – Daniel Cristofani Feb 6 at 2:16
3
\$\begingroup\$

Perl 6, 26 bytes

{$_*12-[-1,3,3,1][$_]}o*-1

Try it online!

If this could be zero-indexed the o*-1 at the end can be removed. Returns (n-1)*12, offsetting the first 4 values.

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

Python 3, 40 36 bytes

lambda n:~-n*12-(*n*[0],1,3,3,-1)[4]

Try it online!

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

Python 3, 39 bytes

lambda n:n>4and~-n*12or[1,9,21,35][n-1]

Try it online!

Total rip of Level River St's answer so upvote him.

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

Python, 38 bytes

lambda n:n*12-11-([1]*n+[2,4,4,0])[-n]

Try it online!

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

C (gcc), 34 33 32 bytes

Saved a byte thanks to Grimmy!!!

f(n){n=--n<4?"!)5C"[n]-32:n*12;}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 32 \$\endgroup\$ – Grimmy Feb 5 at 10:20
  • \$\begingroup\$ @Grimmy Nice - takes care of all the n-1s with a decrement up front. Thanks! :-) \$\endgroup\$ – Noodle9 Feb 5 at 11:52
1
\$\begingroup\$

J, 14 bytes

12(*|@-3!~])<:

Try it online!

A J port of Bubbler's APL answer, so it's also a port of Jonathan Allan's Jelly answer.

\$\endgroup\$
1
\$\begingroup\$

brainfuck, 78 bytes

++++[->+++<]>>>+>--->--->-<<<<<<,-[->>>[+]<[->+<]<[->+>+<<]<[->+<]>]>>[->+<]>.

You can try it online over at TIO (the input is a line-feed (ascii 10) and the output is an l (ascii 108))

You can also try the verbose code at this online interpreter where input can be inserted as decimals, e.g. \6 gives 6 as input. After running, you can hit the "view memory" button and check the value of the output cell in bold, to ensure the result is right.

\$\endgroup\$
1
\$\begingroup\$

brainfuck, 61 bytes

,-[>++<-[>+++<-[>+++>++<<-[>+++>+<<-[>+++<-]]]]]>[>++++<-]>+.

Input/output as character codes (meta).

Try it online! or try it with decimal I/O.

\$\endgroup\$
  • \$\begingroup\$ Good work on this one! \$\endgroup\$ – RGS Feb 6 at 0:21
0
\$\begingroup\$

Rust - 52 bytes

let m=|n|(match n{1=>1,2=>9,3=>21,4=>35,_=>n}-1)*12;

oh gosh.. it loses even to Brainf***

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