13
\$\begingroup\$

Challenge

Imagine a hexagonal grid as shown below. Let's call such a grid has size \$n\$ if it has \$n\$ dots on one side. The following is one of size 3:

  - - -
 - - - -
- - - - -
 - - - -
  - - -

Then, pick as many dots as possible on the grid so that no two dots are adjacent. For size 3, the maximum is 7 dots:

  - * -          * - *
 * - - *        - - - -
- - * - -  or  * - * - *
 * - - *        - - - -
  - * -          * - *

Your task is to output such a grid. For a size-\$n\$ hexagon, the output must contain exactly A002061(n) = \$n^2-n+1\$ non-adjacent dots. The corresponding maximal pattern can be found in this image linked on the OEIS sequence (imagine this: dissect all the hexagons there into triangles, remove one outermost layer of triangles, and pick the centers of original hexagons). The corresponding ASCII-art output must look like the following, modulo rotation/reflection:

n = 1
*

n = 2
 * - 
- - *
 * - 

n = 3 (following or alternative shown above)
  * - *  
 - - - - 
* - * - *
 - - - - 
  * - *  

n = 4
   * - - *   
  - - * - -  
 - * - - * - 
* - - * - - *
 - * - - * - 
  - - * - -  
   * - - *   

n = 5
    - * - - *    
   * - - * - -   
  - - * - - * -  
 - * - - * - - * 
* - - * - - * - -
 - * - - * - - * 
  - - * - - * -  
   * - - * - -   
    - * - - *    

n = 6
     - * - - * -     
    * - - * - - *    
   - - * - - * - -   
  - * - - * - - * -  
 * - - * - - * - - * 
- - * - - * - - * - -
 * - - * - - * - - * 
  - * - - * - - * -  
   - - * - - * - -   
    * - - * - - *    
     - * - - * -     

n = 7
      * - - * - - *      
     - - * - - * - -     
    - * - - * - - * -    
   * - - * - - * - - *   
  - - * - - * - - * - -  
 - * - - * - - * - - * - 
* - - * - - * - - * - - *
 - * - - * - - * - - * - 
  - - * - - * - - * - -  
   * - - * - - * - - *   
    - * - - * - - * -    
     - - * - - * - -     
      * - - * - - *      

n = 8
       - * - - * - - *       
      * - - * - - * - -      
     - - * - - * - - * -     
    - * - - * - - * - - *    
   * - - * - - * - - * - -   
  - - * - - * - - * - - * -  
 - * - - * - - * - - * - - * 
* - - * - - * - - * - - * - -
 - * - - * - - * - - * - - * 
  - - * - - * - - * - - * -  
   * - - * - - * - - * - -   
    - * - - * - - * - - *    
     - - * - - * - - * -     
      * - - * - - * - -      
       - * - - * - - *       

n = 9
        - * - - * - - * -        
       * - - * - - * - - *       
      - - * - - * - - * - -      
     - * - - * - - * - - * -     
    * - - * - - * - - * - - *    
   - - * - - * - - * - - * - -   
  - * - - * - - * - - * - - * -  
 * - - * - - * - - * - - * - - * 
- - * - - * - - * - - * - - * - -
 * - - * - - * - - * - - * - - * 
  - * - - * - - * - - * - - * -  
   - - * - - * - - * - - * - -   
    * - - * - - * - - * - - *    
     - * - - * - - * - - * -     
      - - * - - * - - * - -      
       * - - * - - * - - *       
        - * - - * - - * -        

n = 10
         * - - * - - * - - *         
        - - * - - * - - * - -        
       - * - - * - - * - - * -       
      * - - * - - * - - * - - *      
     - - * - - * - - * - - * - -     
    - * - - * - - * - - * - - * -    
   * - - * - - * - - * - - * - - *   
  - - * - - * - - * - - * - - * - -  
 - * - - * - - * - - * - - * - - * - 
* - - * - - * - - * - - * - - * - - *
 - * - - * - - * - - * - - * - - * - 
  - - * - - * - - * - - * - - * - -  
   * - - * - - * - - * - - * - - *   
    - * - - * - - * - - * - - * -    
     - - * - - * - - * - - * - -     
      * - - * - - * - - * - - *      
       - * - - * - - * - - * -       
        - - * - - * - - * - -        
         * - - * - - * - - *      

I/O and rules

You can use any two distinct non-whitespace chars for marked and unmarked dots respectively. Trailing spaces on each line and leading/trailing whitespaces are allowed. Outputting a list of lines, and outputting integer charcodes instead of the corresponding chars are also allowed.

Standard rules apply. The shortest code in bytes wins.

\$\endgroup\$

4 Answers 4

5
\$\begingroup\$

Charcoal, 33 bytes

NθG↙↘→→↗↖θ⎇﹪賓⟲∧LVoIG;”“⟲∧⦄≧Σ¶ζ;

Try it online! Link is to verbose version of code. Explanation: After reading in n, the code simply draws a hexagon using one of two fill patterns depending on whether n is a multiple of 3; if it is, the fill pattern has the * one - in from the corner, otherwise it has the * in the top left corner.

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

Python 3.8 (pre-release), 92 bytes

f=lambda n,i=0:[l:=' '*(n-i)+('- * - '*n)[n%3%2+i<<1:][:n+i<<1]]+(n>i+1and[*f(n,i+1),l]or[])

Try it online!

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

Haskell, 84 bytes

f n=[[" * - -"!!mod(i+gcd 3n)6|i<-[1,3..2*y]++[4*y..2*y+4*n-4]]|y<-abs<$>[1-n..n-1]]

Try it online!

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

Jelly,  27  26 bytes

3ḍ6_BṙN€ṁ"Ḥ’rƊK€ṭ"Ḷ⁶ẋƊṚŒḄY

A full program that accepts a positive integer and prints using 0 as * and 1 as -.

Try it online! Or see a few with * and - in place of 0 and 1.

How?

3ḍ6_BṙN€ṁ"Ḥ’rƊK€ṭ"Ḷ⁶ẋƊṚŒḄY - Main Link: positive integer, n
3ḍ                         - three divides (n)? -> 1 if n is a multiple of three else 0
  6                        - six
   _                       - subtract -> 5 if n is a multiple of three else 6
    B                      - to binary -> 101 (i.e. "-*-") ...else 110 (i.e. "--*")
      N€                   - negate each (of implicit [1..n]) -> [-1,-2,...,-n]
     ṙ                     - rotate (110 or 101) left by (each of those) -> Patterns (one for each row of bottom half of a hexagon)
             Ɗ             - last three links as a monad, f(n):
          Ḥ                -   double -> 2n
           ’               -   decrement -> 2n-1
            r              -   (2n-1) inclusive range (n) -> [2n-1..n]
         "                 - zip (Patterns) and (range) applying, f(Pattern, i): 
        ṁ                  -   mould like -> resized rows using repetition
              K€           - join each with space characters -> Rows
                     Ɗ     - last three links as a monad, f(n):
                  Ḷ        -   lowered range -> [0..n-1]
                   ⁶       -   space character
                    ẋ      -   repeat -> leading spaces for bottom half
                 "         - zip (Rows) and (leading spaces) applying, f(row, spaces): 
                ṭ          -   tack (row) to (spaces)
                      Ṛ    - reverse -> top of the hexagon
                       ŒḄ  - bounce -> whole hexagon
                         Y - join with newline characters
                           - implicit, smashing print
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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