Concentric rings on a snub square tiling

21

2

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.

Peter Kagey

Posted 2020-02-04T21:25:49.980

Reputation: 2 789

3This looks like a lion! +1 – RGS – 2020-02-04T21:42:09.633

3Can this be zero indexed? – Jo King – 2020-02-04T22:23:54.773

1@JoKing, I'd like to keep it one-indexed—partly because folks have already submitted solutions with that assumption. – Peter Kagey – 2020-02-04T22:26:01.180

Answers

7

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

Jonathan Allan

Posted 2020-02-04T21:25:49.980

Reputation: 67 804

Ah, cool that Jelly has a way to avoid duplicating . I still have no idea how any of it works =p – Grimmy – 2020-02-04T23:34:49.033

Yeah even with Lynn's excellent tutorial and its Chains section, it's still a tricky beast :)

– Jonathan Allan – 2020-02-04T23:43:44.433

26

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

Level River St

Posted 2020-02-04T21:25:49.980

Reputation: 22 049

Really nice job :D – RGS – 2020-02-04T23:24:14.297

Well done on finding a way to simplify the topology and providing a closed formula for n>4! – G0BLiN – 2020-02-05T17:52:07.427

9

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}$$

Arnauld

Posted 2020-02-04T21:25:49.980

Reputation: 111 334

9

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α

Grimmy

Posted 2020-02-04T21:25:49.980

Reputation: 12 521

7

Python, 31 bytes

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

Try it online!

xnor

Posted 2020-02-04T21:25:49.980

Reputation: 115 687

4

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

Nick Kennedy

Posted 2020-02-04T21:25:49.980

Reputation: 11 829

1Here’s 9 bytes – Grimmy – 2020-02-04T23:17:19.500

1@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. – Nick Kennedy – 2020-02-04T23:24:41.330

@Grimmy I was trying to golf exactly that, and now managed it :) – Jonathan Allan – 2020-02-04T23:31:19.250

4

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

Bubbler

Posted 2020-02-04T21:25:49.980

Reputation: 16 616

4

brainfuck, 50 bytes

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

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

Daniel Cristofani

Posted 2020-02-04T21:25:49.980

Reputation: 947

1

You can remove leading >> by using the implementation available on TIO.

– Bubbler – 2020-02-06T01:46:40.117

2That'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. – Daniel Cristofani – 2020-02-06T02:16:24.190

3

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.

Jo King

Posted 2020-02-04T21:25:49.980

Reputation: 38 234

3

Python 3, 40 36 bytes

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

Try it online!

mabel

Posted 2020-02-04T21:25:49.980

Reputation: 1 489

2

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.

Noodle9

Posted 2020-02-04T21:25:49.980

Reputation: 2 776

2

Python, 38 bytes

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

Try it online!

Jonathan Allan

Posted 2020-02-04T21:25:49.980

Reputation: 67 804

2

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!

Noodle9

Posted 2020-02-04T21:25:49.980

Reputation: 2 776

132 – Grimmy – 2020-02-05T10:20:57.730

@Grimmy Nice - takes care of all the n-1s with a decrement up front. Thanks! :-) – Noodle9 – 2020-02-05T11:52:48.970

1

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.

Galen Ivanov

Posted 2020-02-04T21:25:49.980

Reputation: 13 815

1

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.

RGS

Posted 2020-02-04T21:25:49.980

Reputation: 5 047

1

brainfuck, 61 bytes

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

Input/output as character codes (meta).

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

Grimmy

Posted 2020-02-04T21:25:49.980

Reputation: 12 521

Good work on this one! – RGS – 2020-02-06T00:21:10.107

0

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***

don bright

Posted 2020-02-04T21:25:49.980

Reputation: 1 189