16
\$\begingroup\$

Given, in any way and order,

  • dimensions (in whichever order you want)

  • a probability (in whatever format you want)

  • number of iterations (you may choose whether to count the initial state or not)

do one of the following:

  • animate, or

  • output each step of, or

  • return a list of states of

a spaces-filled sky of those dimensions.

For each iteration:

  • every sky character has the given probability of becoming a *

  • the eight characters that surround any * become
    \ | /
    -      -
    / | \, if they don't become stars, however,

  • if a character is within range of two *s, make it an X

Example

The 7×22 sky (frame for clarity only – don't output the frame)

┌──────────────────────┐
│                      │
│                      │
│                      │
│                      │
│                      │
│                      │
│                      │
└──────────────────────┘

could with 1% probability become

┌──────────────────────┐
│  *                   │
│                      │
│                      │
│                      │
│                      │
│           *          │
│                      │
└──────────────────────┘

and the next step

┌──────────────────────┐
│ - - *                │
│ /|\                  │
│          *           │
│                      │
│          \|/         │
│          - -         │
│          /|\         │
└──────────────────────┘

and then

┌──────────────────────┐
│    - -               │
│    /|\  \|/          │
│         - -          │
│         /|\          │
│                      │
│      * *             │
│                      │
└──────────────────────┘

and

┌──────────────────────┐
│                      │
│                      │
│                      │
│                      │
│     \|X|/            │
│     - X -            │
│     /|X|\          * │
└──────────────────────┘

and so on

┌──────────────────────┐
│                      │
│                      │
│                      │
│                   *  │
│                      │
│                   \|/│
│                   - -│
└──────────────────────┘
\$\endgroup\$
3
  • 5
    \$\begingroup\$ If someone viewing this is not familiar at all about the USA culture, they might be confused to why you are launching fireworks on 4th July. \$\endgroup\$
    – Okx
    Jul 4, 2017 at 17:56
  • \$\begingroup\$ @Okx to destroy all who oppose, of course! \$\endgroup\$ Jul 17, 2017 at 21:29
  • 1
    \$\begingroup\$ Obligatory xkcd \$\endgroup\$ Jul 19, 2017 at 14:22

2 Answers 2

5
\$\begingroup\$

ES6, 520 496 bytes

Clipping and everything else should work now.

(w,h,p,i,r=0,s=($=>{while(++r<i)for(j=0,_=$[r]=[],z=$[r-1]||[];j<w*h;){k=j+1,l='*',c=0
n=(j%w&&z[j-1]==l&&++c)|(k%w&&z[k]==l&&++c)?'-':' '
n=(z[j-w]==l&&++c)|(z[j+w]==l&&++c)?'|':n
n=(j%w&&z[j-w-1]==l&&++c)|(k%w&&z[k+w]==l&&++c)?'\\':n
n=(k%w&&z[k-w]==l&&++c)|(j%w&&z[j+w-1]==l&&++c)?'/':n
_[j++]=Math.random()<p?l:c>1?'X':n}})(x=[])||x)=>{c=document.body.children[0],r=0;while(++r<i)setTimeout((k=0)=>{for(r++,c.innerHTML='';k<h;)c.innerHTML+=s[r].slice(k*w,++k*w).join('')+'\n'},90*r);r=0}

View animation!

Saved 24 bytes thanks to Zacharý's tip.

Old solution, 478 bytes (with clipping bug)

I think I got all the rules correct, however the solution has a clipping problem where everything exiting right/left wraps around one line lower/higher on the opposite side.

(w,h,p,i,r=0,s=($=>{while(++r<i)for(j=0,_=$[r]=[],z=$[r-1]||[];j<w*h;){c=0
n=(z[j-1]=='*'&&++c)|(z[j+1]=='*'&&++c)?'-':' '
n=(z[j-w]=='*'&&++c)|(z[j+w]=='*'&&++c)?'|':n
n=(z[j-w-1]=='*'&&++c)|(z[j+w+1]=='*'&&++c)?'\\':n
n=(z[j-w+1]=='*'&&++c)|(z[j+w-1]=='*'&&++c)?'/':n
_[j++]=Math.random()<p?'*':c>1?'X':n}})(x=[])||x)=>{c=document.body.children[0],r=0;while(++r<i)setTimeout((k=0)=>{for(r++,c.innerHTML='';k<h;)c.innerHTML+=s[r].slice(k*w,++k*w).join('')+'\n'},90*r);r=0}

View old.

\$\endgroup\$
4
  • \$\begingroup\$ Nicely done. Can't you fix the clipping by appending blank columns on the sides, animate, and then chop? \$\endgroup\$
    – Adám
    Jul 4, 2017 at 21:24
  • \$\begingroup\$ Couldn't you define a variable to be '*' since you use it SO often? \$\endgroup\$
    – Adalynn
    Jul 5, 2017 at 21:28
  • \$\begingroup\$ @Zacharý Good tip, I will define variables for '*' and other commonly used things. \$\endgroup\$
    – 2ndAttmt
    Jul 5, 2017 at 22:07
  • \$\begingroup\$ If only JS had macros. \$\endgroup\$
    – Adalynn
    Jul 5, 2017 at 22:35
3
\$\begingroup\$

APL (Dyalog), 65 chars or 71 bytes*

Prompts for dimensions (rows, columns), then for iterations (not counting initial state), then for probability (as n in ¹⁄).

⎕{1=?⍺⍺:'*'⋄2≤+/b←'*'=1↓4⌽,⍵:'X'⋄⊃b/'-/|\\|/-'}⌺3 3{⍺⍺⎕←⍵}⍣⎕⊢⎕⍴''

Try it online!

⎕⍴'' prompt for input and use that to reshape an empty string, padding with spaces as needed

 yield that

⍣⎕ prompt for input and apply the following function on the above () that many times:

 …{} derive a function using the below function as operand (⍺⍺), as follows:

  ⎕←⍵ print the argument

  ⍺⍺ apply the following function:

   …⌺3 3 apply the following function on each element's 3×3 Moore neighborhood:

    ⎕{} get input and use it as operand (⍺⍺) to derive a new function

     ?⍺⍺ random integer among the first ⍺⍺ integers

     1= Boolean if equal to one

     : if true:

      '*' return a star

      else:

      ,⍵ ravel (flatten) the argument (the Moore neighborhood)

      4⌽ rotate it cyclically four steps to the left

      1↓ drop one element (the original center)

      '*'= Boolean list where equal to a star

      b← store that as b

      +/ sum that

      2≤ Boolean if two or higher

      : if true:

       'X' return an X

       else:

       b/'-/|\\|/-' use the b to to filter the string

        pick the first one, if there are none, pick the prototype (a space)


* To run in Dyalog Classic, simply replace with ⎕U233A .

\$\endgroup\$

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.