42
\$\begingroup\$

Print a continuous sinusoidal wave scrolling vertically on a terminal. The program should not terminate and should continuously scroll down the wave (except until it is somehow interrupted). You may assume overflow is not a problem (i.e. you may use infinite loops with incrementing counters, or infinite recursion).

The wave should satisfy the following properties:

  • Amplitude = 20 chars (peak amplitude)
  • Period = 60 to 65 lines (inclusive)
  • The output should only consist of spaces, newline and |
  • After each line of output, pause for 50ms

Sample output:

                    |
                      |
                        |
                          |
                            |
                              |
                                |
                                 |
                                   |
                                    |
                                     |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
            |
          |
        |
       |
     |
    |
   |
  |
 |
 |
 |
 |
 |
 |
 |
  |
   |
    |
     |
      |
        |
         |
           |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                             |
                              |
                                |
                                  |
                                   |
                                    |
                                      |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                    |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
           |
          |
        |
       |
     |
    |
   |
  |
 |
 |
 |
 |
 |
 |
 |
  |
   |
    |
     |
       |
        |
          |
            |
             |
               |
                 |
                   |

The above output should go on forever unless otherwise interrupted, e.g. SIGINT or SIGKILL, or closing terminal window, or you power off your machine, or the Sun swallows the Earth, etc.

Shortest code wins.

Note. I am aware of a similar problem on Display Scrolling Waves but this isn't exactly the same. In my problem, the wave is not to be scrolled "in place" - just output it on a terminal. Also, this is an ascii-art problem, so don't use Mathematica to plot it.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ Peak amplitude, peak-to-peak amplitude, or root-square amplitude? \$\endgroup\$
    – DavidC
    Jan 16, 2014 at 20:31
  • \$\begingroup\$ Peak amplitude. \$\endgroup\$
    – user12205
    Jan 16, 2014 at 20:38
  • 1
    \$\begingroup\$ Is it ok to draw a wave with just |s and no spaces? \$\endgroup\$
    – Gelatin
    Jan 17, 2014 at 1:23
  • 1
    \$\begingroup\$ All answers yet are invalid. They also stop for SIGKILL not just for SIGINT. \$\endgroup\$
    – bot47
    Apr 4, 2014 at 9:02
  • 1
    \$\begingroup\$ @Max Ried fine, I will change it to "should go on forever unless otherwise interrupted". \$\endgroup\$
    – user12205
    Apr 4, 2014 at 10:28

39 Answers 39

1
2
0
\$\begingroup\$

Groovy 87 Chars

Okay, now for a serious answer that actually follows the guidelines

def x=0;while(true){println ' '*(20*Math.sin(x)+20)+'|';x+=0.1;Thread.sleep(50)}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Do you think a suitable decimal approximation for Math.PI/30 can save you a few chars? \$\endgroup\$
    – user12205
    Jan 17, 2014 at 16:53
0
\$\begingroup\$

CJam, 42 bytes

CJam is a bit younger than this challenge, but since this submission doesn't beat APL anyway, I don't see a problem with that.

es{es1$m50>{500d/ms21*K+S41*\i'|tN+oes}*}h

This won't work in the online interpreter, so you'll have to use the Java one.

The basic idea is to keep a timestamp on the stack, and whenever 50 ms have passed since the last timestamp, a new line is printed (where the sine is computed from that last time), and a new timestamp left on the stack.

\$\endgroup\$
0
\$\begingroup\$

Pascal: 99 characters

uses Crt;var i:Real;begin
repeat
Writeln('|':Round(Sin(i)*20+21));i:=i+0.1;Delay(500)until 0=1;end.

Another task where Pascal's output formatting comes handy and everything else is pain.

\$\endgroup\$
0
\$\begingroup\$

Julia, 60 chars

Unlike CCP’s solution, mine uses a recursive function:

a(n)=(print(" "^int(20sin(.1n)+20)*"|\n");sleep(.05);a(n+1))

Example:

julia> a(1)
                      |
                        |
                          |
                            |
                              |
                               |
                                 |
                                  |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                  |
                                |
                              |
                             |
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 44 bytes

@L
A=A+.1?" "*(COS(A)*20+20);"|
WAIT 3GOTO@L
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 57 bytes

for(;;sleep -m 50){' '*(20*[math]::Sin(($i+=.1))+20)+'|'}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 20 bytes

['|NT/Ž20*21+ú,50.W

Try it online.

Explanation:

[               # Start an infinite loop:
 '|            '#  Push character "|"
 N              #  Push the 0-based loop-index
  T/            #  Divide it by 10
    Ž          #  Take the sine of that
      20*       #  Multiply it by 20
         21+    #  Add 21
            ú   #  Pad that many leading spaces before the "|"
                #  (`ú` will ignore any decimal digits and interpret it as an integer)
             ,  #  Pop and print this string with trailing newline
 50.W           #  Sleep 50 ms
\$\endgroup\$
0
\$\begingroup\$

Japt, 20 bytes

Open your browser's console.

50i@Ol|ùMsU°/A Ñ0+21

Test it

\$\endgroup\$
0
\$\begingroup\$

Scala, 92 82 bytes

Saved bytes thanks to the comments.

Golfed version. Try it online!

def f={var i=0;while(i<101){println(" "*(21+(math.sin(i*.1)*20).toInt)+"|");i+=1}}

//math.sin
//scala.math.sin
//Math.sin
//java.lang.Math.sin
//WHATEVER

Ungolfed version. Try it online!

import scala.math._
import scala.concurrent.duration._

object SineWave extends App {
  var i = 0

  def animateWave(): Unit = {
    //while (true) {
    while (i<=100) {
      val j = 21 + (sin(i * 0.1) * 20).toInt
      println(" " * j + "|")
      i += 1
      //Thread.sleep(50)
    }
  }

  animateWave()
}
\$\endgroup\$
1
  • \$\begingroup\$ I think you should be able to do math.sin instead of scala.math.sin. Also, I think f can be def f=, not sure if that's allowed though. \$\endgroup\$
    – user
    May 11, 2023 at 3:15
1
2

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.