Skip to main content
added 1312 characters in body
Source Link
randomra
  • 20.9k
  • 4
  • 46
  • 111

J, 37 34 40 bytes

1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

I will add explanation tomorrow.Explanation (from left to right):

  • i: generates list -n, -(n-1), ..., n-1, n

  • ( )"*/~@i: creates the Descartes product of i: with itself in a matrix arrangement, e.g. for n = 1 creates the following 3-by-3 matrix

     ┌─────┬────┬────┐
     │-1 -1│-1 0│-1 1│
     ├─────┼────┼────┤
     │0 -1 │0 0 │0 1 │
     ├─────┼────┼────┤
     │1 -1 │1 0 │1 1 │
     └─────┴────┴────┘
    
  • for every matrix-element with integers x y we do the following

  • +&|,-,+,[,] calculate a list of properties

    • +&| abs(x)+abs(y), equals 0 iff (if and only if) x=0 and y=0
    • - x-y, equals 0 iff x=y i.e. we are on the diagonal
    • + x+y, equals 0 iff x=-y i.e. we are on the anti-diagonal
    • [ x, equals 0 iff x=0 i.e. we are on the middle row
    • ] y, equals 0 iff y=0 i.e. we are on the middle column
  • 'O\/-|'#~0= compare these above property values to 0 and take the ith character from the string 'O\/-|' if the ith property is true.

  • the first character in the resulting string will always be the one we need, if there string is empty we need a space

  • {. takes the first character of a string and if there is no one it returns a space character as padding just as we need

  • we now have the exact matrix we need so we print it to stdout once with 1:echo

Try it online here.

J, 37 34 40 bytes

1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

I will add explanation tomorrow.

Try it online here.

J, 37 34 40 bytes

1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

Explanation (from left to right):

  • i: generates list -n, -(n-1), ..., n-1, n

  • ( )"*/~@i: creates the Descartes product of i: with itself in a matrix arrangement, e.g. for n = 1 creates the following 3-by-3 matrix

     ┌─────┬────┬────┐
     │-1 -1│-1 0│-1 1│
     ├─────┼────┼────┤
     │0 -1 │0 0 │0 1 │
     ├─────┼────┼────┤
     │1 -1 │1 0 │1 1 │
     └─────┴────┴────┘
    
  • for every matrix-element with integers x y we do the following

  • +&|,-,+,[,] calculate a list of properties

    • +&| abs(x)+abs(y), equals 0 iff (if and only if) x=0 and y=0
    • - x-y, equals 0 iff x=y i.e. we are on the diagonal
    • + x+y, equals 0 iff x=-y i.e. we are on the anti-diagonal
    • [ x, equals 0 iff x=0 i.e. we are on the middle row
    • ] y, equals 0 iff y=0 i.e. we are on the middle column
  • 'O\/-|'#~0= compare these above property values to 0 and take the ith character from the string 'O\/-|' if the ith property is true.

  • the first character in the resulting string will always be the one we need, if there string is empty we need a space

  • {. takes the first character of a string and if there is no one it returns a space character as padding just as we need

  • we now have the exact matrix we need so we print it to stdout once with 1:echo

Try it online here.

added 4 characters in body
Source Link
randomra
  • 20.9k
  • 4
  • 46
  • 111

J, 37 34 3840 bytes

1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

I will add explanation tomorrow.

Try it online here.

J, 37 34 38 bytes

echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

I will add explanation tomorrow.

Try it online here.

J, 37 34 40 bytes

1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

I will add explanation tomorrow.

Try it online here.

added 41 characters in body
Source Link
randomra
  • 20.9k
  • 4
  • 46
  • 111

J, 37 3434 38 bytes

echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

I will add explanation tomorrow.

Try it online here.

J, 37 34 bytes

('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2
\ | /
 \|/ 
--O--
 /|\ 
/ | \

I will add explanation tomorrow.

Try it online here.

J, 37 34 38 bytes

echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

I will add explanation tomorrow.

Try it online here.

added 4 characters in body
Source Link
randomra
  • 20.9k
  • 4
  • 46
  • 111
Loading
deleted 10 characters in body
Source Link
randomra
  • 20.9k
  • 4
  • 46
  • 111
Loading
deleted 4 characters in body
Source Link
randomra
  • 20.9k
  • 4
  • 46
  • 111
Loading
Source Link
randomra
  • 20.9k
  • 4
  • 46
  • 111
Loading