17
\$\begingroup\$

This:

enter image description here

is a Glider.

In Conway's Game of Life, the glider is a famous pattern that rapidly traverses across the board. For today's challenge, we are going to draw an ASCII art Game of Life Board, and place a glider on it.

The board we are starting with is this:

|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|

This board is made up entirely of pipes | and underscores _, and is 10x10. You must write a program or function that takes in two integers, 'x' and 'y', and outputs this same board with a glider at those coordinates. For example, if you had a glider at position (1, 1) (0-indexed), you must output the following:

|_|_|_|_|_|_|_|_|_|_|
|_|_|*|_|_|_|_|_|_|_|
|_|_|_|*|_|_|_|_|_|_|
|_|*|*|*|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|

You can assume that the glider will never be placed out of bounds, so both x and y will always be in the [0-7] range. You may also choose to take the coordinates 1-indexed, but you must specify this in your answer. In this, case the inputs will always be in the [1-8] range. Here are some examples (all 0-indexed):

0, 0:
|_|*|_|_|_|_|_|_|_|_|
|_|_|*|_|_|_|_|_|_|_|
|*|*|*|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|

7, 7:
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|*|_|
|_|_|_|_|_|_|_|_|_|*|
|_|_|_|_|_|_|_|*|*|*|

7, 4:
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|*|_|
|_|_|_|_|_|_|_|_|_|*|
|_|_|_|_|_|_|_|*|*|*|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|

5, 2:
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|*|_|_|_|
|_|_|_|_|_|_|_|*|_|_|
|_|_|_|_|_|*|*|*|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|

As usual, you may take your IO in any reasonable format. This includes, but is not limited to a string with newlines, an array of strings, a 2d array of strings, or writing to a file/STDOUT. You may also choose what order to take x and y in.

Since this is , standard loopholes are banned, and make the shortest code that you can!

\$\endgroup\$
  • \$\begingroup\$ Can we change which corner identifies the glider's position? \$\endgroup\$ – Stephen May 31 '17 at 20:15
  • \$\begingroup\$ @StephenS No, the coordinates should Identify where the top left corner of the glider starts. \$\endgroup\$ – DJMcMayhem May 31 '17 at 20:18
  • \$\begingroup\$ Related. \$\endgroup\$ – MD XF May 31 '17 at 20:42
  • 2
    \$\begingroup\$ the glider is a famous pattern that slowly traverses across the boar.. Slowly? It is the fastest diagonal moving object in GoL. It reaches 1/4 of the speed of light. \$\endgroup\$ – Christoph Jun 1 '17 at 7:26
  • 1
    \$\begingroup\$ @Christoph, good point,we can observe that it's length does not appear to contract in the direction of travel, but I don't know any way of measuring it's mass at relativistic speeds. \$\endgroup\$ – Wossname Jun 1 '17 at 10:39

13 Answers 13

7
\$\begingroup\$

MATL, 35 32 30 bytes

20*+'|_'5E:21:I$)42b' 34'Q+(

The code contains unprintable chars. Input is 0-based. Try it online!

\$\endgroup\$
5
\$\begingroup\$

V, 31, 30 bytes

10O±°_|ÀGjjÀ|3r*kr*kh.Í*ü_/|&

Try it online!

Hexdump:

00000000: 3130 4fb1 b05f 7c1b c047 6a6a c07c 3372  10O.._|..Gjj.|3r
00000010: 2a6b 722a 6b68 2ecd 2afc 5f2f 7c26       *kr*kh..*._/|&

This takes input as program arguments, and 1-indexed.

Explanation:

10O                         " On the following 10 lines, insert:
   ±°_                      "   10 '_' characters
      |                     "   And a '|'
       <esc>                " Return to normal mode
            ÀG              " Go to the a'th line
              jj            " Move down two lines
                À|          " Go to the b'th column
                  3r*       " and replace the next 3 characters with asterisks
                     k      " Move up a line
                      r*    " And replace this char with an asterisk
                        kh  " Move up a line and to the left
                          . " And repeat the last change we performed (replace with asterisk)
                            "
Í                           " On every line, substitute:
 *                          "   An asterisk
  ü                         "   OR
   _                        "   An underscore
    /                       " With:
     |&                     "   A bar followed by the matched pattern
\$\endgroup\$
  • \$\begingroup\$ Two questions: What? and How? \$\endgroup\$ – Pureferret Jun 1 '17 at 8:31
  • 1
    \$\begingroup\$ @Pureferret two answers: Golf languages, SCIENCE (or he wrote V XD) \$\endgroup\$ – Christopher Jun 1 '17 at 11:45
  • 1
    \$\begingroup\$ @Pureferret I've added an explanation. \$\endgroup\$ – DJMcMayhem Jun 1 '17 at 17:00
2
\$\begingroup\$

Jelly, 37 35 bytes

ȷ2b1
Ḍ+“£Æßæç‘Ṭ+¢s⁵j3;0$€Fṭ0ị“_*¶|”

Try it online!

How it works

ȷ2b1                             - the literal [1,1,1,1,...,1,1,1] with 100 elements
Ḍ+“£Æßæç‘Ṭ+¢s⁵j3;0$€Fṭ0ị“_*¶|”   - input (x,y)
Ḍ                                - convert (x,y) to 10*x+y
 +                               - add, to get the five "*" positions,
  “£Æßæç‘                        - the literal [2,13,21,22,23]
         Ṭ                       - return an array with those positions as truthy elements
          +¢                    - Now we format: pad to length 100 with the above literal
            s⁵j3                 - add newlines (represented by 3) to each set of 10
                ;0$€F            - add pipes (represented by 0) to each
                     ṭ0          - add a 0 to the beginning
                       ị“_*¶|”   - index into the string “_*¶|”
\$\endgroup\$
  • 1
    \$\begingroup\$ How do you type/generate your programs? \$\endgroup\$ – RobotCaleb Jun 1 '17 at 3:23
  • 1
    \$\begingroup\$ @RobotCaleb : generally copy-pasting from the Jelly codepage. I run them at the TIO nexus when not at my main machine, and a clone of the Jelly repository at my main computer. \$\endgroup\$ – fireflame241 Jun 1 '17 at 3:27
2
\$\begingroup\$

Python 2, 151 bytes

Will golf more.

def f(x,y):r,x=[list('|_'*10+'|')for i in[1]*10],x*2;r[y][x+3]=r[y+1][x+5]=r[y+2][x+1]=r[y+2][x+3]=r[y+2][x+5]='*';print'\n'.join(''.join(i)for i in r)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ If you're willing to switch to Python 3, you can save 3 bytes by using [*'|_'*10+'|'] instead of the list() call. \$\endgroup\$ – L3viathan Jun 1 '17 at 8:10
2
\$\begingroup\$

Perl 6, 88 bytes

->\x,\y{(^10 »*»i X+ ^10).map:{<|* |_>[$_!=
(1-2i|2-i|0|1|2)+x+y*i+2i]~"|
"x(.re==9)}}
  • Complex numbers are used to represent the coordinates.

  • ^10 »*» i X+ ^10 generates the grid of all complex numbers with integer components from zero through nine.

  • Returns a list of strings, each one holding one line.

\$\endgroup\$
  • \$\begingroup\$ Interesting, I didn't know that Perl utilizes non-ASCII. What does » do? How is it encoded? \$\endgroup\$ – DJMcMayhem Jun 1 '17 at 1:13
  • 1
    \$\begingroup\$ It's just Unicode's RIGHT POINTING GUILLEMET, U+00BB, encoded in UTF-8. In Perl 6 it can just as well be written as two angle brackets, >>, but that's the same number of bytes as the guillemet so for golfing I prefer the latter as it looks a little classier IMHO. As for what it does, it transforms the operator it surrounds into a "hyperoperator" that applies pairwise to the lists or values on either side. Here, it multiplies each element of the range 0-9 by i, giving 0, i, 2i, ..., 9i. \$\endgroup\$ – Sean Jun 1 '17 at 4:46
1
\$\begingroup\$

Haskell, 96 bytes

r=[0..9]
x#y=['|':(r>>=(\i->[last$'_':['*'|elem(i-x,j-y)$zip[1,2,0,1,2][0,1,2,2,2]],'|']))|j<-r]

Takes in two integers (x and y) and returns a list of Strings, i.e. a 2D list of type [[Char]].


Test suite:

import System.Environment

main :: IO ()
main = do
    args <- getArgs
    let (x, y) = (read $ args !! 0, read $ args !! 1)
    mapM_ putStrLn (x#y)
\$\endgroup\$
1
\$\begingroup\$

Ruby, 87 bytes

->x,y{[a=(b='|_')*10+?|]*y+%w(|_|*|_ |_|_|* |*|*|*).map{|r|b*x+r+b*(7-x)+?|}+[a]*(7-y)}

Try it online!

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

Mathematica, 115 113 bytes

x(a="|_"~Table~10~Table~10;(a[[##]]="|*")&@@(#+x)&/@({{0,1,2,2,2},{1,2,0,1,2}});""<>Riffle[#<>"|"&/@a,"\n"])

where

This takes input in {row, col} format, and is 1-indexed, but can be turned into 0-indexed without adding bytes.

Some notes:

  1. \n is a newline character, takes 1 byte.
  2. is \[Function], takes 3 bytes.
  3. is \[Transpose], takes 3 bytes.

Note that "array of string" is allowed, so I can just remove Riffle, gives

Mathematica, 98 97 bytes

x(a="|_"~Table~10~Table~10;(a[[##]]="|*")&@@(#+x)&/@({{0,1,2,2,2},{1,2,0,1,2}});#<>"|"&/@a)
\$\endgroup\$
1
\$\begingroup\$

Java 8, 165 144 bytes

x->y->{String r="";for(int i=0,j;++i<11;r+="\n")for(r+="|",j=0;++j<11;)r+=(i==x&j==y+1)|(i==x+1&j==y+2)|(i==x+2&j>=y&j<y+3)?"*|":"_|";return r;}

Explanation:

Try it here.

x->y->{                  // Method with two integer parameters and String return-type
  String r="";           //  Result-String
  for(int i=0,j;++i<11;  //  Loop (1) over the rows
     r+="\n|")           //    And after each iteration add a new-line to the result-String
    for(r+="|",          //   Start by appending "|" at the start of the line
      j=0;++j<11;        //   Loop (2) over the columns
      r+=(i==x&j==y+1)|(i==x+1&j==y+2)|(i==x+2&j>=y&j<y+3)?
                         //    If this coordinate should contain a '*'
        "*|"             //     Append "*|"
       :                 //    Else:
        "_|"             //     Append "_|"
    );                   //   End of loop (2)
                         //  End of loop (1) (implicit / single-line body)
  return r;              //  Return the result-String
}                        // End of method
\$\endgroup\$
  • 1
    \$\begingroup\$ Explanation has different code than your answer? Look at first assignment to r. \$\endgroup\$ – Computronium Jun 1 '17 at 13:00
  • \$\begingroup\$ @Computronium Oops, thanks for noticing, fixed. The bye-count was correct, explanation was correct, TIO-link was correct, but the actual answer was still the old incorrect one.. \$\endgroup\$ – Kevin Cruijssen Jun 1 '17 at 13:15
1
\$\begingroup\$

JavaScript (ES6), 99 bytes

x=>y=>eval('for(i=0,o="";i<101;o+=((d=i-x-y*10)==1|d==12|d>19&d<23?"|*":"|_")+(++i%10?"":`|\n`))o')

Takes input via currying: f(5)(2) for x=5, y=2. Coordinates are zero-indexed.

Test Snippet

f=
x=>y=>eval('for(i=0,o="";i<101;o+=((d=i-x-y*10)==1|d==12|d>19&d<23?"|*":"|_")+(++i%10?"":`|\n`))o')

xi.oninput=yi.oninput=_=>O.innerHTML=f(xi.value)(yi.value)
O.innerHTML=f(xi.value=5)(yi.value=2)
<style>*{font-family:Consolas;}input{width:2.5em;}</style>
x: <input id="xi" type="number" min="0" max="7">,
y: <input id="yi" type="number" min="0" max="7">
<pre id="O">

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

SOGL, 23 bytes

LIΖ|_ΟL∙.«."¾'┼ΞΧ⌠²‘5nž

note: this expects input to be 1-indexed

Explanation:

LI                       push 11
  Ζ|_                    push "|" and "_"
     Ο                   make an altrenates string [with 11 separators, which are "|" and parts "_"]
      L∙                 get an array of 10 of those
        .«               take input and multiply by 2 (x pos)
          .              take input (y pos)
           "¾'┼ΞΧ⌠²‘     push "_|*|__|_|**|*|*" - the glider in the map
                    5n   split into an array with items of length 5
                      ž  replace in the 1st [grid] array at positions [inp1*2, inp2] to 2nd array [glider]
\$\endgroup\$
0
\$\begingroup\$

Python 2, 133 bytes

x,y=input()
a=[[z for z in'_'*10]for o in'|'*10]
b=a[y+2]
a[y][x+1]=a[y+1][x+2]=b[x]=b[x+1]=b[x+2]="*"
for b in a:print o+o.join(b)+o

Try it online!

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

Charcoal, 28 bytes

UO²¹χ|_J×N²N“ "}IyE%%mLBMφ/”

Try it online! Link to verbose mode for description.

\$\endgroup\$
  • \$\begingroup\$ I'm disappointed that Charcoal isn't, well... ASCII-only ;) \$\endgroup\$ – Beta Decay Jun 18 '17 at 21:35
  • \$\begingroup\$ You can remove the |_ before the \n to save a compressed byte. (I tried several different ways of printing the glider but none saved any bytes.) \$\endgroup\$ – Neil Dec 25 '17 at 0:59

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.