6
\$\begingroup\$

You love watching the snow falling outside, but you know it won't be snowing much longer. You could take a video, but decide to make a snow animation because ... um ... you need to practice your animation skills!

Snow

Snow is simply the * symbol here. Half of the snow moves at 1 char per frame, while the other half moves at 0.5 chars per frame. The program ends when all the snow reaches the bottom of the screen. The viewing area you will be using is 80x25. You will have 100 snow flakes to output, half fast, and half slow.

Every frame you will dispense 4 flakes, 2 fast and 2 slow ones, at random locations at the top of the viewing area, until you have used all 100. The slow flakes can move either move the first frame, stay still the next, or stay still the first frame, and move the next, it doesn't matter. Overlapping flakes don't have anything special, just the * like the rest.

Other notes

  • You must make a full program, no functions here.
  • Your program must run close to 10fps (+-1 frame per sec).
  • You can assume the console is the same size as the viewing area.
  • Each frame you must remove the previous output, either by pushing all previous output off the top of the console, or by clearing the output completely.

Example

To make this shorter this just outputs 2 flakes per frame (1 fast, 1 slow), only 20 flakes will be outputted, and the viewing area is 20x5.

Frame 1:
    *              *




Frame 2:
    * *            *
    *               



Frame 3:
    *  *          * 
      *            *
    *               


Frame 4:
   *   *      *     
    *             **
      *             
    *               

Frame 5:
   *            *  *
    *  *      *     
                  **
      *             

Frame 6:
        *      **   
   *   *           *
    *         *    *
                  * 

Frame 7:
     *  **          
   *           **   
    *  *           *
              *    *

Frame 8:
    *    **         
     *  *       *   
   *   *       *    
    *              *

Frame 9:
 *  *         *     
        ***         
   * *          *   
    *  *       *    

Frame 10:
 *              **  
    *    *    *     
        * *     *   
   * * *            

Frame 11:
                *   
 *  *            *  
        **    *     
   *      *     *   

Frame 12:

 *              *   
    *    *       *  
        *     * *   

Frame 13:

                *   
 *  *               
        **       *  

Frame 14:


 *              *   
    *    *          

Frame 15:


                *   
 *  *               

Frame 16:



 *              *   

Frame 17:



                *   

If you want some snow/rain confusion and clean code, go here

\$\endgroup\$
2
  • \$\begingroup\$ I think a lot of this(/your) challenges would be better of with actual graphic output. Many languages don't really have any tools for ASCII art output and building strings for this gets tedious very fast. And since that pixel rotating challenge, it is clear that even GO can be golfed very well. \$\endgroup\$
    – user42643
    Commented Feb 2, 2016 at 17:16
  • 1
    \$\begingroup\$ Perhaps, but I don't want to clutter the challenge with a second output format, and even if I did it would be an uneven playing field. Besides, you can say the same thing about graphics for some languages (e.g. java). \$\endgroup\$
    – J Atkin
    Commented Feb 2, 2016 at 17:19

3 Answers 3

1
\$\begingroup\$

Octave, 179 bytes

A=' *';C=D=E=zeros(25,80);B=[i=1,1,(1:78)*0];do if i++>25 B=B&0;end
C=[B(randperm(80));C(1:24,:)];D=[B(randperm(80));D(1:24,:)];F=D;D=E;E=F;pause(.1);disp(A((F|=C|D)+1))
until(~F)

Ungolfed

A=' *';                               % Index with 1,2 to translate to ' ','*'
                                      % A([2 1 2 2 1]) => '* ** '
C=D=E=zeros(25,80);                   % Locations of fast snowflakes,
                                      % slow ones moving on frame 1,3,5...
                                      % and slow ones moving on 2,4,6...
B=[1,1,zeros(1,78)];                  % Line with two snowflakes in it.
                                      % Will be added on top each frame.
i=1;
do
  if i++>25                           % We push 4 snowflakes per iteration.
    B=B&0;                            % Stop new input after 25 iterations.
  end
  C=[B(randperm(80));C(1:24,:)];      % Shift fast snowflakes down
                                      % and add randomly permuted input line.
  D=[B(randperm(80));D(1:24,:)];      % Shift one set of slow snowflakes down
                                      % and add randomly permuted input line.
  F=D;D=E;E=F;                        % Swap the two sets of slow snowflakes.
  pause(.1);
  disp(A((C|D|E)+1))                  % Print '*' where either of the matrices
                                      % is set, ' ' otherwise.
until(~(C|D|E))                       % Loop until the matrices are all zero
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 370 bytes

(S=(n,o,r="")=>{for(i=0;i<w;i++)o[i][0].pop(),o[i][0].unshift(0),Math.abs(n/4%2)&&(o[i][1].pop(),o[i][1].unshift(0));if(n>0)for(i=0;i<4;i++)o[Math.random()*w|0][i%2][0]++;for(j=0;j<h;j++){for(i=0;i<w;i++)r+=o[i][0][j]+o[i][1][j]>0?"*":" ";r+=`
`}console.log(r),n>2*-t&&setTimeout(g=>S(n-4,o),t)})(t=100,[...new Array(w=80)].map(i=>[0,0].map(i=>new Array(h=25).fill(0))))

Simulates with a multidimensional array, then plays it out. Could probably be golfed more.

Here's a slightly less compressed version:

(S=(c,v,o='')=>{
  for(i=0;i<w;i++){
    v[i][0].pop();v[i][0].unshift(0);
    if(Math.abs(c/4%2)){
      v[i][1].pop();v[i][1].unshift(0)
    }
  }
  if(c>0){for(i=0;i<4;i++){v[Math.random()*w|0][i%2][0]++}}
  for(j=0;j<h;j++){
    for(i=0;i<w;i++){
      o+=v[i][0][j]+v[i][1][j]>0?'*':' ';
    }
    o+=`
`}
  console.log(o);
  if(c>-t*2){setTimeout(g=>S(c-4,v),t)}
})(t=100,[...new Array(w=80)].map(a=>[0,0].map(b=>new Array(h=25).fill(0))))
\$\endgroup\$
1
\$\begingroup\$

C, 295 282 bytes

#include <stdlib.h>
main(){int z=25,q=100,h=50,w=30,m[q][w],s=0,a[q],b[q],x,y;for(;s<q;s++){memset(m,32,sizeof m);system("cls");a[s]=rand()%w;b[s]=rand()%w;for(x=0,y=s;y&&x<h;)m[y/2][b[x]]=m[--y][a[x++]]=42;for(x=0;x<z;x++){for(y=0;y<w;y++)putchar(m[x][y]);printf("\n");}sleep(1);}}

I've used Dev-C++/Windows to run.

Ungolfed version:

#include <stdlib.h>
main(){
    int z=25,                  // number of rows
        q=100,                 // number of flakes
        h=50,                  // number of flakes/2
        w=30,                  // number of columns
        m[q][w],               // matrix to print
        s=0,                   // counter
        a[q],                  // fast flakes
        b[q],                  // slow flakes
        x,y;                   // aux
    for(;s<q;s++){             // until the last slow flake takes the floor
        memset(m,32,sizeof m);   // clear the matrix (' '==32(ascii))
        system("cls");           // clear windows console
        a[s]=rand()%w;           // random (s*2-1)º fast flake position
        b[s]=rand()%w;           // random (s*2)º slow flake position
        for(x=0,y=s;y&&x<h;){    // set each flake position in matrix:
            m[y/2][b[x]]=94;       // slow flakes ('^'==94(ascii))
            m[--y][a[x++]]=42;     // fast flakes ('*'==42(ascii))
        }                        //
        printf("Frame %i:\n",s); // print frame number (step order)
        for(x=0;x<z;x++){        // for each row in matrix
            printf("%02i:",x);     // print the row index
            for(y=0;y<w;y++)       // for each column in row
                putchar(m[x][y]);    // print slow/fast flake or space
            printf("\n");          // for each row follows a new line
        }                        // then
        sleep(1);                // wait one second till next step
    }
}

Notes:

  1. Maybe you'll need to maximize the console.
  2. In the ungolfed code I did some extra prints for better visualization. Also used ^ for slow flakes.
\$\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.