Place a glider!

17

1

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!

James

Posted 2017-05-31T20:12:35.567

Reputation: 54 537

Can we change which corner identifies the glider's position? – Stephen – 2017-05-31T20:15:35.503

@StephenS No, the coordinates should Identify where the top left corner of the glider starts. – James – 2017-05-31T20:18:42.817

Related. – MD XF – 2017-05-31T20:42:52.223

2the 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. – Christoph – 2017-06-01T07:26:04.590

1@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. – Wossname – 2017-06-01T10:39:28.543

Answers

7

MATL, 35 32 30 bytes

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

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

Luis Mendo

Posted 2017-05-31T20:12:35.567

Reputation: 87 464

5

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

James

Posted 2017-05-31T20:12:35.567

Reputation: 54 537

Two questions: What? and How? – Pureferret – 2017-06-01T08:31:00.457

1@Pureferret two answers: Golf languages, SCIENCE (or he wrote V XD) – Christopher – 2017-06-01T11:45:54.423

1@Pureferret I've added an explanation. – James – 2017-06-01T17:00:02.403

2

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 “_*¶|”

fireflame241

Posted 2017-05-31T20:12:35.567

Reputation: 7 021

1How do you type/generate your programs? – RobotCaleb – 2017-06-01T03:23:46.607

1

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

– fireflame241 – 2017-06-01T03:27:17.483

2

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!

totallyhuman

Posted 2017-05-31T20:12:35.567

Reputation: 15 378

If you're willing to switch to Python 3, you can save 3 bytes by using [*'|_'*10+'|'] instead of the list() call. – L3viathan – 2017-06-01T08:10:06.320

2

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.

Sean

Posted 2017-05-31T20:12:35.567

Reputation: 4 136

Interesting, I didn't know that Perl utilizes non-ASCII. What does » do? How is it encoded? – James – 2017-06-01T01:13:33.047

1It'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. – Sean – 2017-06-01T04:46:26.247

1

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)

user202729

Posted 2017-05-31T20:12:35.567

Reputation: 14 620

1

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)

Augmented Fourth

Posted 2017-05-31T20:12:35.567

Reputation: 71

1

Java 10, 165 144 138 135 bytes

x->y->{var 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;}

-6 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<11;  //  Loop over the rows:
      r+="\n|")          //    After every iteration: add a newline to the result-String
    for(r+="|",          //   Start by appending "|" at the start of the line
        j=0;++j<11;      //   Inner loop 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 "_|"
  return r;}             //  Return the result-String

Kevin Cruijssen

Posted 2017-05-31T20:12:35.567

Reputation: 67 575

1Explanation has different code than your answer? Look at first assignment to r. – Computronium – 2017-06-01T13:00:33.473

@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.. – Kevin Cruijssen – 2017-06-01T13:15:01.593

1

Ruby, 87 bytes

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

Try it online!

G B

Posted 2017-05-31T20:12:35.567

Reputation: 11 099

1

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

Justin Mariner

Posted 2017-05-31T20:12:35.567

Reputation: 4 746

0

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]

dzaima

Posted 2017-05-31T20:12:35.567

Reputation: 19 048

0

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!

ElPedro

Posted 2017-05-31T20:12:35.567

Reputation: 5 301

0

Charcoal, 28 bytes

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

Try it online! Link to verbose mode for description.

ASCII-only

Posted 2017-05-31T20:12:35.567

Reputation: 4 687

I'm disappointed that Charcoal isn't, well... ASCII-only ;) – Beta Decay – 2017-06-18T21:35:19.300

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.) – Neil – 2017-12-25T00:59:12.723