Show me some fireworks!

16

4

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

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

Adám

Posted 2017-07-04T17:53:01.953

Reputation: 37 779

5If 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. – Okx – 2017-07-04T17:56:24.250

@Okx to destroy all who oppose, of course! – Magic Octopus Urn – 2017-07-17T21:29:21.727

1Obligatory xkcd – caird coinheringaahing – 2017-07-19T14:22:48.083

Answers

5

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.

2ndAttmt

Posted 2017-07-04T17:53:01.953

Reputation: 131

Nicely done. Can't you fix the clipping by appending blank columns on the sides, animate, and then chop? – Adám – 2017-07-04T21:24:28.527

Couldn't you define a variable to be '*' since you use it SO often? – Zacharý – 2017-07-05T21:28:19.497

@Zacharý Good tip, I will define variables for '*' and other commonly used things. – 2ndAttmt – 2017-07-05T22:07:35.837

If only JS had macros. – Zacharý – 2017-07-05T22:35:13.567

2

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 .

Adám

Posted 2017-07-04T17:53:01.953

Reputation: 37 779