Code Golf Asked by FlipTack on January 20, 2021
Your challenge is to make an infinite loading screen, that looks like this:
Or, to be more specific:
Loading...
, with a trailing space, but no trailing newline.|
, /
, -
and
: every 0.25 seconds, overwrite the last one with the next in the sequence. You can overwrite just the last character, or delete and rewrite the whole line, as long Loading...
remains unchanged.stdout
.Here is the C++ code I used to create the example (ungolfed):
#include <iostream>
#include <string>
#include <thread>
using namespace std;
int main() {
string cycle = "|/-\";
int i = 0;
cout << "Loading... ";
while (true) {
// Print current character
cout << cycle[i];
// Sleep for 0.25 seconds
this_thread::sleep_for(chrono::milliseconds(250));
// Delete last character, then increase counter.
cout << "b";
i = ++i % 4;
}
}
May the best golfer win!
print'Loading...';for(;;)'|/-\'.any{print"$itb";sleep 250}
(modified to work with later versions of groovy, removed ascii cast)
Answered by Matias Bjarland on January 20, 2021
An anonymous function that takes no input and outputs to the console in graphics mode. Does not function with TIO.
Clear Screen
?"Loading..."
Do
?@(1,11)Mid$("|/-\",i+1,1)
Wait.25
i=Mod(i+1,4)
Loop
Answered by Taylor Scott on January 20, 2021
-t.03
flag, 33 bytes'o!vooooo|-/ ...gnidaoL
{[4<o8o:
The only part of the specification this doesn't implement is that the first frame should be printed as soon as the program is run which seems rather strict and unobservable. The first frame here take 1.08 seconds to print, and every frame after that takes 0.24 seconds.
Answered by Jo King on January 20, 2021
@Oq Oo`LoÃHg... `+"|/-\"gW°%4)1
a#úiU
@Oq Oo`LoÃHg... `+"|/-\"gW°%4)1
a#úiU
@ Declare a function...
Oq that clears the screen,
Oo"Loading... " prints this string,
+"|/-\"gW++%4 plus the spinner char (using a variable W),
)1 and finally returns 1
and implicitly store this function to U.
a Call U once (return 1 is needed for this)
#úiU and call U every 250 milliseconds.
By the nature of Japt syntax, it's impossible to call U()
directly (well, it's possible using JS directive $...$
but it's too long after all.) So we use U.a()
method that calls U
with numbers from 0 to infinity until U
returns true. If you omit )1
at the end of the first line and try running it, your browser will hang.
Without the initial U()
call, the output window will show the implicit output (seemingly random integer value) before first showing the Loading...
text.
Finally, a#úiU
actually translates to U.a(250 .i(U))
, which registers the 250ms loop first and then passes the return value (which happens to be undefined
) to U.a
(which accepts optional function argument, but seems to do nothing special with undefined
).
Answered by Bubbler on January 20, 2021
This certainly isn't the shortest code to do this. I like doing coding challenges, so I'm posting it anyway.
#include <iostream>
#include <string>
#include <thread>
using namespace std;int main(){string s = "|/-\";int i=0;cout<<"Loading... ";while(1){cout<<s[i];this_thread::sleep_for(chrono::milliseconds(99));cout<<"b";i=++i%4;}}
Answered by Colin Robertson on January 20, 2021
setInterval(e=>a.innerHTML='|/-\'[++i%4],i=250)
Loading... <a id=a>-
Answered by copyreplace on January 20, 2021
while(!usleep(25e4))echo"rLoading... ",'|/-'[@$i++%4];
Answered by kip on January 20, 2021
DECLARE @ CHAR W:SET @=IIF(@='/','-',IIF(@='-','',IIF(@='','|','/')))PRINT'Loading... '+@ WAITFOR DELAY'0:0:0.25'GOTO W
Answered by WORNG ALL on January 20, 2021
void f()throws Exception{for(int k=0;;k++){System.out.print("rLoading...
"+"|/-\".charAt(k%=4));Thread.sleep(250);}}
Gif animation:
Answered by Twometer on January 20, 2021
Loading... A⁰αHW²⁵⁰¦¹«A⁺¹ααP§|/-α
Try it online! Refresh command has changed since so it is different on TIO. Link to verbose code for explanation.
Answered by ASCII-only on January 20, 2021
i;f(){for(;write(1,"rLoading... -b\b|b/",13+i%8);i++)usleep(1<<17);}
Tested on SystemResque-Cd 4.9.6 in this program:
#include <stdio.h>
#include <unistd.h>
/*
i;
f(){
for(
;
write(1, "rLoading... -b\b|b/", 13+i%8);
i++
){
usleep(1<<17);
}
}
*/
i;f(){for(;write(1,"rLoading... -b\b|b/",13+i%8);i++)usleep(1<<17);}
int main(){
f();
return 0;
}
compiled with gcc 4.9.4
Answered by epimatech on January 20, 2021
print!("Loading... ");fn f(){let a="\|/-";for n in 0 .. 4{std::thread::sleep_ms(250);print!("x08{}",a[n]);}f();}f;
Fixed some compile errors. Nowhere near a compiler right now so bear with me.
Answered by Chad Baxter on January 20, 2021
i;f(){O;;i=4)Wi--)dR2,"rLoading... %c","|\-/"[i]),U250000
Answered by MD XF on January 20, 2021
i;f(){for(;;i=4)while(i--)dprintf(2,"rLoading... %c","|\-/"[i]),usleep(250000);}
Answered by MD XF on January 20, 2021
C++, 109 Bytes
There should be a lot to improve, it's my first golf code. Feedback is greatly appreciated :)
#import<stdio.h>
#import<unistd.h>
int b;main(){for(;;usleep(2500))printf("rLoading...%c","-\|/"[++b%=4]);}
Answered by Clémox on January 20, 2021
f=c=>setTimeout(f,250,-~c%4,o.value="Loading... "+"|/-\"[~~c])
<input id=o
(f=c=>setTimeout(f,250,-~c%4,o.value="Loading... "+"|/-\"[~~c]))()
<input id=o>
Answered by Shaggy on January 20, 2021
Possibly the only way I can do this, as PKod only has one write-able variable
lL=oo=ao=do=io=no=go=.ooo =|oyw=/oyw=-oyw=oywl<
Explanation: l - Clear screen and since its the first char, allow printing no operation (NOP) chars
L - NOP, thus print "L"
=oo=ao=do=io=no=go=.ooo - Set as certain chars and print them.
(space) - Another NOP, thus print a space
=|oyw - Set char as "|", print it, then wait a quarter of a second and remove it
=/oyw=-oyw=oywl - Same as above, with different symbols to match the challenge
< - Go back to the start
"Gif" (more like mp4): https://i.gyazo.com/577dd164313a6b2e5dbf40249efb435d.mp4
You can see quote marks around the code in the console, thats because cmd tries to do stuff with my "<" and would return an error. It's just to nicely pass the code to the interpeter without cmd interfering.
Answered by P. Ktinos on January 20, 2021
0 A$="/|-":A=1:FORI=0TO1STEP0:PRINT"{home}LOADING..."MID$(A$,A,1):A=A+1:GOSUB2:ON-(A>4)GOTO0
1 NEXT
2 FORX=0TO99:NEXT:RETURN
Using print 38911-(fre(0)-65536*(fre(0)<0))
tells me that I have consumed 123 bytes of the computers memory (on the C64); this will probably work on other variants of Commodore BASIC, such as the BASIC 7; you will need to use BASIC keyword abbreviations to enter it on a real C64 or VIC-20.
In order to make it infinite*, you will need to disable the RUN/STOP key with a POKE, I think it's POKE 808,234
- that will mean you can't break into the BASIC listing without an Action Replay or a soft reset or something. The time delay can be altered in line 2 - increase the FOR X counter as required.
Answered by Shaun Bebbers on January 20, 2021
@L CLS?"Loading... "+"|/-"[I]:I=(I+1)MOD 4 WAIT 15 GOTO@L
Ungolfed:
@L 'loop start
CLS 'clear console
?"Loading... "+"|/-"[I] 'construct our output and print
I=(I+1)MOD 4 'inc counter, MOD to prevent overflow
GOTO @L 'loop
Answered by snail_ on January 20, 2021
I know a C# solution has already been posted, but this one is a fully functional console program (including usings) in less bytes!
class P{static void Main(){for(int i=0;;){System.Console.Write("rLoading... "+@"|/-"[i=i++==3?0:i]);System.Threading.Thread.Sleep(250);}}}
class P
{
static void Main()
{
for (int i = 0; ;)
{
System.Console.Write("Loading... "+@"|/-"[i = i++ == 3 ? 0 : i]);
System.Threading.Thread.Sleep(250);
}
}
}
Yes, I'm probably very late, but I had to post it..!
EDIT: I figured someone else posted a 109 byte C# solution, oh well
EDIT 2: Thanks to the poster of the 109 byte C# solution, I managed to lose 3 more bytes by removing i<4
from my for
loop, thanks!
EDIT 3: Removed C# 6 string interpolation and used good old +
instead to save 2 more bytes.
EDIT 4: Not declaring a var
for the animation characters anymore, instead I added them directly into the Write()
method, saving another 8 bytes
EDIT 5: Removed the parameter string[]s
from the Main method to save 9 bytes!
EDIT 6: Used carriage return instead of System.Console.Clear()
, removed a using and moved the incrementing of i
+ the ternary inside of System.Console.Write()
, all thanks to @CSharpie! (all this saved 21 bytes)
Answered by Metoniem on January 20, 2021
while [ 1 ]; do for i in `echo '|/-' | grep -o .`; do printf $'rLoading...'$i;sleep 0.25;done;done
This is not nicely golfed, so please tell me where I can improve here.
This does work, and has been tested on a Raspbian Raspberry Pi, an Amazon server, and an Ubuntu machine. This would not work on a Solaris machine because the sleep
command on those systems cannot take inputs less than 1.
Answered by ckjbgames on January 20, 2021
CLS?"Loading... ";"|-/"[3AND MAINCNT/15]EXEC.
Answered by 12Me21 on January 20, 2021
while 1 {lmap c {| / - \} {puts -nonewline stderr rLoading... $c;after 250}}
assuming I can write to stderr
instead of stdout
Answered by sergiol on January 20, 2021
while 1 {lmap c {| / - \} {puts -nonewline rLoading...$c;flush stdout;after 250}}
Can be seen running on: https://goo.gl/BJmxV0
Answered by sergiol on January 20, 2021
:ClrHome
:Disp "LOADING...
:For(A,1,5
:A-4(A=5→A
:Output(1,12,sub("+/-*",A,1
:Pause .25
:End
Many commands in TI-Basic are 1-2 byte tokens, which may make it appear to be a byte miscount.
Due to TI-Basic's very limited character set, the following characters have been replaced: |
with +*
.
This will only run correctly on the newest version of a TI-84+ CE or CSE.
Answered by Julian Lachniet on January 20, 2021
In awk, with some help from ANSI codes and the rotor comes piped in:
{while(i=1+i%4)print"Loading... "$i"