26
\$\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\$
5
  • \$\begingroup\$ Can we change which corner identifies the glider's position? \$\endgroup\$
    – Stephen
    May 31, 2017 at 20:15
  • \$\begingroup\$ @StephenS No, the coordinates should Identify where the top left corner of the glider starts. \$\endgroup\$
    – DJMcMayhem
    May 31, 2017 at 20:18
  • \$\begingroup\$ Related. \$\endgroup\$
    – MD XF
    May 31, 2017 at 20:42
  • 4
    \$\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, 2017 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, 2017 at 10:39

22 Answers 22

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\$
6
\$\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\$
0
4
\$\begingroup\$

Java 10, 165 144 138 135 129 127 125 bytes

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

1-indexed.

-14 bytes thanks to @ceilingcat.

Explanation:

Try it here.

x->y->{               // Method with two integer parameters and String return-type
  var r="";           //  Result-String, starting empty
  for(int i=0,j;i<10  //  Loop over the rows:
      ;               //    After every iteration:
       r+="|\n")      //     Append a "|" and newline to the result-String
    for(j=0;++j<11;)  //   Inner loop over the columns:
      r+=i==x&j==y|i==x+1&++j==y|i==x+2&j<y+3&j>=y?
                      //    If this coordinate should contain a '*'
        "|*"          //     Append "|*" to the result-String
       :              //    Else:
        "|_");        //     Append "|_" to the result-String
  return r;}          //  Return the result-String
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Explanation has different code than your answer? Look at first assignment to r. \$\endgroup\$ Jun 1, 2017 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\$ Jun 1, 2017 at 13:15
3
\$\begingroup\$

Perl 5, 67 bytes

sub{map{($_-join('',@_)~~[1,12,20..22]?"|*":"|_")."|\n"x/9$/}0..99}

Try it online!

Input Parameters: y, x

\$\endgroup\$
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\$
2
  • 1
    \$\begingroup\$ How do you type/generate your programs? \$\endgroup\$
    – RobotCaleb
    Jun 1, 2017 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\$ Jun 1, 2017 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\$
1
  • \$\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, 2017 at 8:10
2
\$\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\$
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\$
2
  • \$\begingroup\$ Interesting, I didn't know that Perl utilizes non-ASCII. What does » do? How is it encoded? \$\endgroup\$
    – DJMcMayhem
    Jun 1, 2017 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, 2017 at 4:46
2
\$\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\$
1
  • \$\begingroup\$ You can save 1 byte if split the sum tio.run/… \$\endgroup\$
    – EzioMercer
    Jan 30, 2023 at 8:52
2
\$\begingroup\$

APL (Dyalog Extended), 40 bytes

'*'@((⍳2)∘ר⎕∘+¨⍸⊤1 5 3)⊢'|',⍨10 20⍴'|_'

Try it online!

A full program, accepting x and y as input.

Calculate coordinates, and uses @ to modify them.

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

Python 3, 97 bytes

def f(x,y):
 b=(["|_"]*10+["|\n"])*10
 for c in[1,13,22,23,24]:b[c+11*y+x]="|*"
 return"".join(b)

Try it online!

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

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

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\$
1
\$\begingroup\$

Zsh, 85 bytes

try it online!

for i (2 13 21 22 23)G[i+$1$2]=*
for i ({1..100})printf \|${G[i]:-_}&&((i%10))||<<<\|

Saved 12 bytes by moving the glider char * assignments to a function. Saved another 4 bytes by removing the function and adding a ternary to the printf stuff (at the expense of readability!). Saved another 7 by inlining some data instead of constructing an array. Saved another 10 by simplifying some expressions. Removed {} for another -1. Saved 11 by using parameter expansion ${name:-word} instead of convoluted ascii logic.
130 bytes 118 bytes 114 bytes 107 bytes 97 bytes 96 bytes

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

JavaScript, 98 bytes

Based on this answer

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

We can save 1 byte if use o+=a,o+=b instead of o+=(a)+(b)

Try it:

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

JavaScript, 101 bytes

This answer may be more efficient with another pattern

x=>y=>eval("for(i=0,s='';i<101;s+=[1,12,20,21,22].includes(i-x-y*10)?'|*':'|_',s+=++i%10?'':`|\n`)s")

Try it:

f=
x=>y=>eval("for(i=0,s='';i<101;s+=[1,12,20,21,22].includes(i-x-y*10)?'|*':'|_',s+=++i%10?'':`|\n`)s")

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

Charcoal, 28 bytes

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

Try it online! Link to verbose mode for description.

\$\endgroup\$
2
  • \$\begingroup\$ I'm disappointed that Charcoal isn't, well... ASCII-only ;) \$\endgroup\$
    – Beta Decay
    Jun 18, 2017 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, 2017 at 0:59
0
\$\begingroup\$

Python 2, 97 bytes

def f(x,y):
 b=(["|_"]*10+["|\n"])*10
 for c in[1,13,22,23,24]:b[c+11*y+x]="|*"
 return"".join(b)

Try it online!

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

QBasic, 113 107 bytes

Uses 0-indexing.

INPUT x,y
s=11*y+x
FOR i=1TO 110
?"|"+CHR$(95+53*((i=s+2)+(i=s+14)-(i>s+22)*(i<s+26))+85*(i/11=i\11));
NEXT

We model the grid as 110 |s each followed by some character, for which we calculate the ASCII code:

  • Every 11th character is a newline (character code 95 - 85 = 10)
  • If we calculate the start index of the glider as s = 11 * y + x, then indices s + 2, s + 14, s + 23, s + 24, and s + 25 are asterisks (character code 95 - 53 = 42)
  • The rest are underscores (character code 95)

Original version using LOCATE (1-indexed):

INPUT x,y
CLS
FOR i=0TO 9
FOR j=0TO 9
?"|_";
NEXT
?"|
NEXT
LOCATE y,2*x+2
?"*
LOCATE,2*x+4
?"*
LOCATE,2*x
?"*|*|*
\$\endgroup\$
0
\$\begingroup\$

Canvas, 24 bytes

|_A×|+A* *¶  *¶***∙*┘«╶╋

Try it here!

Takes 1-indexed position in form

x
y

as input.

Only beaten by dzaima's SOGL answer at time of writing.

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

05AB1E, 25 (or 24?) bytes

„|_ти„|*•A|,•₂вI+ǝTôJ€Ć»

0-based indexing.

Try it online or verify all test cases.

Could be 1 byte less (the J) if we could take the input-coordinates joined together as single integer:
Try it online or verify all test cases.

Explanation:

„|_            # Push string "|_"
   ти          # Repeat it 100 times as list of strings
„|*            # Push string "|*"
   •A|,•       # Push compressed integer 681976
        ₂в     # Convert it to base-26 as list: [1,12,20,21,22]
          IJ   # Push the input-pair, and join it together to an integer
            +  # Add it to each value in the list [1,12,20,21,22]
„|*          ǝ # Insert the "|*" string at those (0-based) indices to the list of "|_"
Tô             # Split the list into 10 parts of 10 strings each
  J            # Join each inner list together to a string
   €           # Map over each line:
    Ć          # Enclose; append its own head (the "|")
     »         # Join the list of strings by newlines
               # (After which the result is output implicitly)

See this 05AB1E tip of mine (sections How to compress large integers? and How to compress integer lists?) to understand why •A|,• is 681976 and •A|,•₂в is [1,12,20,21,22].

\$\endgroup\$

Your Answer

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

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