title: Code Golf: New Year's Fireworks
body: <p>The year 2009 is coming to an end, and with the economy and all, we'll save our money and instead of buying expensive fireworks, we'll celebrate in ASCII art this year.</p>
  
  <h2>The challenge</h2>
  
  <p>Given a set of fireworks and a time, take a picture of the firework at that very time and draw it to the console.</p>
  
  <p>The best solution entered before midnight on New Year's Eve (UTC) will receive a bounty of 500 rep. This is code golf, so the number of characters counts heavily; however so do community votes, and I reserve the ultimate decision as to what is best/coolest/most creative/etc.</p>
  
  <h2>Input Data</h2>
  
  <p>Note that our coordinate system is left-to-right, bottom-to-top, so all fireworks are launched at a <code>y</code>-coordinate of 0 (zero).</p>
  
  <p>The input data consists of fireworks of the form</p>
  
  <pre><code>(x, speed_x, speed_y, launch_time, detonation_time)
  </code></pre>
  
  <p>where </p>
  
  <ul>
  <li><code>x</code> is the position (column) where the firework is launched,</li>
  <li><code>speed_x</code> and <code>speed_y</code> are the horizontal and vertical velocity of the firework at launch time,</li>
  <li><code>launch_time</code> is the point in time that this firework is launched,</li>
  <li><code>detonation_time</code> is the point in time that this firework will detonate.</li>
  </ul>
  
  <p>The firework data may be hardcoded in your program as a list of 5-tuples (or the equivalent in your language), not counting towards your character count. It must, however, be easy to change this data.</p>
  
  <p>You may make the following assumptions:</p>
  
  <ul>
  <li>there is a reasonable amount of fireworks (say, fewer then a hundred)</li>
  <li>for each firework, all five numbers are integers within a reasonable range (say, 16 bits would suffice for each),</li>
  <li><code>-20 &lt;= x &lt;= 820</code></li>
  <li><code>-20 &lt;= speed_x &lt;= 20</code></li>
  <li><code>0 &lt; speed_y &lt;= 20</code></li>
  <li><code>launch_time &gt;= 0</code></li>
  <li><code>launch_time &lt; detonation_time &lt; launch_time + 50</code></li>
  </ul>
  
  <p>The single additional piece of input data is the point of time which is supposed to be rendered. This is a non-negative integer that is given to you via standard input or command line argument (whichever you choose).</p>
  
  <p>The idea is that (assuming your program is a python script called <code>firework.py</code>) this bash script gives you a nice firework animation:</p>
  
  <pre><code>#!/bin/bash
  I=0
  while (( 1 )) ; do
      python firework.py $I
      I=$(( $I + 1 ))
  done
  </code></pre>
  
  <p>(feel free to put the equivalent .BAT file here).</p>
  
  <h2>Life of a firework</h2>
  
  <p>The life of a firework is as follows:</p>
  
  <ul>
  <li>Before the launch time, it can be ignored.</li>
  <li>At launch time, the rocket has the position <code>(x, 0)</code> and the speed vector <code>(speed_x, speed_y)</code>.</li>
  <li>For each time step, the speed vector is added to the position. With a little stretch applied to Newton's laws, we assume that the speed stays constant.</li>
  <li>At detonation time, the rocket explodes into nine sparks. All nine sparks have the same position at this point in time (which is the position that the rocket would have, hadn't it exploded),
  but their speeds differ. Each speed is based on the rocket's speed, with -20, 0, or 20 added to <code>speed_x</code> and -10, 0, or 10 added to <code>speed_y</code>. That's nine possible combinations.</li>
  <li>After detonation time, gravity starts to pull: With each time step, the gravitational constant, which happens to be 2 (two), is subtracted from every spark's <code>speed_y</code>.
  The horizontal <code>speed_x</code> stays constant.</li>
  <li>For each time step after the detonation time, you <strong>first</strong> add the speed vector to the position, <strong>then</strong> subtract 2 from <code>speed_y</code>.</li>
  <li>When a spark's <code>y</code> position drops below zero, you may forget about it.</li>
  </ul>
  
  <h2>Output</h2>
  
  <p>What we want is a picture of the firework the way it looks at the given point in time. We only look at the frame <code>0 &lt;= x &lt;= 789</code> and <code>0 &lt;= y &lt;= 239</code>, mapping it to a 79x24 character output.</p>
  
  <p>So if a rocket or spark has the position (247, 130), we draw a character in column 24 (zero-based, so it's the 25th column), row 13 (zero-based and counting from the bottom, so it's line 23 - 13 = 10, the 11th line
  of the output).</p>
  
  <p>Which character gets drawn depends on the current speed of the rocket / spark:</p>
  
  <ul>
  <li>If the movement is horizontal*, i.e. <code>speed_y == 0 or abs(speed_x) / abs(speed_y) &gt; 2</code>, the character is "<code>-</code>".</li>
  <li>If the movement is vertical*, i.e. <code>speed_x == 0 or abs(speed_y) / abs(speed_x) &gt; 2</code>, the character is "<code>|</code>".</li>
  <li>Otherwise the movement is diagonal, and the character is "<code>\</code>" or "<code>/</code>" (you'll guess the right one).</li>
  <li>If the same position gets drawn to more than once (even if it's the same character), we put "<code>X</code>" instead. So assuming you have a spark at <code>(536, 119)</code> and one at <code>(531, 115)</code>, you draw an "<code>X</code>", regardless of their speeds.</li>
  </ul>
  
  <p><sup>* update: these are integer divisions, so the slope has to be at least 3, or at most 1/3, respectively</sup></p>
  
  <p>The output (written to standard output) is 24 lines, each terminated by a newline character. Trailing spaces are ignored, so you may, but don't need to, pad to a width of 79. The lines may not be longer than 79 characters (excluding the newline). All interior spacing must be space characters (ASCII 32).</p>
  
  <h2>Sample Data</h2>
  
  <p>Fireworks:</p>
  
  <pre><code>fireworks = [(628, 6, 6, 3, 33),
               (586, 7, 11, 11, 23),
               (185, -1, 17, 24, 28),
               (189, 14, 10, 50, 83),
               (180, 7, 5, 70, 77),
               (538, -7, 7, 70, 105),
               (510, -11, 19, 71, 106),
               (220, -9, 7, 77, 100),
               (136, 4, 14, 80, 91),
               (337, -13, 20, 106, 128)]
  </code></pre>
  
  <p>Output at time 33:</p>
  
  <pre>
  
  
  
  
  
         \         |         /                                                   
  
  
                                                        /                   \    
  
         -         |         /                                                   
  
  
  
  
         -         |         -                                                   
  
  
                                                        /                   \    
  
  
  
  
  
  </pre>
  
  <p>Output at time 77:</p>
  
  <pre>
  
  
  
  
  
  
  
  
  
  
  
  
                                              \                                  
  
  
  
  
  
  
                                                  \                              
                        X                                                        
  
  
                        \                                                        
  </pre>
  
  <p>Output at time 93:</p>
  
  <pre>
  
  
  
                \   |   /                                                        
  
                \   /   /                                                        
  
                -   -   -              \                                         
  
  
  
  
         \                                                                       
  
  
  
  
  
  
  
    /                               \                               \            
  
  
  
  </pre>
  
  <p><strong>Update:</strong> I have uploaded the expected output at the times 0 thru 99 to <a href="http://firework.%C3%BC-wie-geek.de/0.html" rel="nofollow noreferrer">firework.ü-wie-geek.de/NUMBER.html</a>, where NUMBER is the time. It includes debug information; click on a particle to see its current position, speed, etc. And yes, it's an umlaut domain. If your browser can't handle that (as obviously neither can Stack Overflow), try <a href="http://firework.xn---wie-geek-p9a.de/0.html" rel="nofollow noreferrer">firework.xn---wie-geek-p9a.de</a>.</p>
  
  <p><strong>Another update:</strong> As hinted at in the comments below, a longer firework is now available on <a href="http://www.youtube.com/watch?v=HRYg6ixAnPg" rel="nofollow noreferrer">YouTube</a>. It was created with a modified version of <a href="https://stackoverflow.com/questions/1947031/code-golf-new-years-fireworks/1953608#1953608">MizardX' entry</a>, with a total fireworks count of 170 (yes, that's more than the spec asked for, but the program handled it gracefully). Except for the color, the music, and the end screen, the animation can be recreated by any entry to this code golf. So, if you're geeky enough to enjoy an ASCII art firework (you know you are): Have fun, and a happy new year to all!</p>