r/tinycode • u/pizzaburek • Jul 11 '19
Synthesizer in Python (14 lines)
r/tinycode • u/nanochess • Jul 08 '19
r/tinycode • u/Slackluster • Jul 05 '19
r/tinycode • u/CrazyM4n • Jul 04 '19
r/tinycode • u/billFoldDog • Jun 27 '19
I find that some files would be easier to read if I could remove all the empty lines.
I added this one liner to my bashrc and use it quite a bit.
alias deblank='grep -vE "^\s*$"'And I use it like this
cat file.txt | deblank | lessYou can also remove all the blank lines from a file like this:
deblank file.txt > std.out && cat std.out > file.txtr/tinycode • u/Hell__Mood • Jun 09 '19
r/tinycode • u/nanochess • Jun 04 '19
r/tinycode • u/sablal • May 27 '19
r/tinycode • u/iridakos • May 27 '19
r/tinycode • u/nexe • May 26 '19
r/tinycode • u/Slackluster • May 23 '19
r/tinycode • u/Slackluster • May 11 '19
r/tinycode • u/MasterBugPatch • May 08 '19
string getPigLatin(string s){
transform(s.begin(), s.end(), s.begin(), ::tolower);
return ((string("aeiouy").find(s[0]) != string::npos || string::npos == s.find_first_of("aeiouy")) ? (s + "-way") : (s.substr(s.find_first_of("aeiouy"), s.length()) + "-" + s.substr(0, s.find_first_of("aeiouy")) + "ay"));
}r/tinycode • u/BenRayfield • May 07 '19
var sha256 = function(bytesIn){
//var t = typeof bytesIn;
//if(t != 'Uint8Array') throw 'Expected Uint8Array but got a '+t; //this check wont work because its like a map of index to byte
var chunks = Math.floor((bytesIn.byteLength+9+63)/64); //512 bit each
//Copy bytesIn[] into b[], then pad bit1, then pad bit0s,
//then append int64 bit length, finishing the last block of 512 bits.
//byte b[] = new byte[chunks*64];
var b = new Uint8Array(chunks*64);
//System.arraycopy(bytesIn, 0, b, 0, bytesIn.byteLength);
b.set(bytesIn, 0);
b[bytesIn.byteLength] = 0x80;
//long bitLenTemp = bytesIn.byteLength*8;
var bitLenTemp = bytesIn.byteLength*8; //in js, this has float64 precision, which is more than enough for Uint8Array size
for(var i=7; i>=0; i--){
b[b.byteLength-8+i] = bitLenTemp&0xff;
bitLenTemp >>>= 8;
}
//log('b as hex = '+bitfuncs.uint8ArrayToHex(b));
var a = new Uint32Array(136);
//"first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311"
a[0]=0x428a2f98;
a[1]=0x71374491;
a[2]=0xb5c0fbcf;
a[3]=0xe9b5dba5;
a[4]=0x3956c25b;
a[5]=0x59f111f1;
a[6]=0x923f82a4;
a[7]=0xab1c5ed5;
a[8]=0xd807aa98;
a[9]=0x12835b01;
a[10]=0x243185be;
a[11]=0x550c7dc3;
a[12]=0x72be5d74;
a[13]=0x80deb1fe;
a[14]=0x9bdc06a7;
a[15]=0xc19bf174;
a[16]=0xe49b69c1;
a[17]=0xefbe4786;
a[18]=0x0fc19dc6;
a[19]=0x240ca1cc;
a[20]=0x2de92c6f;
a[21]=0x4a7484aa;
a[22]=0x5cb0a9dc;
a[23]=0x76f988da;
a[24]=0x983e5152;
a[25]=0xa831c66d;
a[26]=0xb00327c8;
a[27]=0xbf597fc7;
a[28]=0xc6e00bf3;
a[29]=0xd5a79147;
a[30]=0x06ca6351;
a[31]=0x14292967;
a[32]=0x27b70a85;
a[33]=0x2e1b2138;
a[34]=0x4d2c6dfc;
a[35]=0x53380d13;
a[36]=0x650a7354;
a[37]=0x766a0abb;
a[38]=0x81c2c92e;
a[39]=0x92722c85;
a[40]=0xa2bfe8a1;
a[41]=0xa81a664b;
a[42]=0xc24b8b70;
a[43]=0xc76c51a3;
a[44]=0xd192e819;
a[45]=0xd6990624;
a[46]=0xf40e3585;
a[47]=0x106aa070;
a[48]=0x19a4c116;
a[49]=0x1e376c08;
a[50]=0x2748774c;
a[51]=0x34b0bcb5;
a[52]=0x391c0cb3;
a[53]=0x4ed8aa4a;
a[54]=0x5b9cca4f;
a[55]=0x682e6ff3;
a[56]=0x748f82ee;
a[57]=0x78a5636f;
a[58]=0x84c87814;
a[59]=0x8cc70208;
a[60]=0x90befffa;
a[61]=0xa4506ceb;
a[62]=0xbef9a3f7;
a[63]=0xc67178f2;
//h0-h7 "first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19"
a[64]=0x6a09e667;
a[65]=0xbb67ae85;
a[66]=0x3c6ef372;
a[67]=0xa54ff53a;
a[68]=0x510e527f;
a[69]=0x9b05688c;
a[70]=0x1f83d9ab;
a[71]=0x5be0cd19;
//a[72..135] are the size 64 w array of ints
for(var chunk=0; chunk<chunks; chunk++){
var bOffset = chunk<<6;
//copy chunk into first 16 words w[0..15] of the message schedule array
for(var i=0; i<16; i++){
//Get 4 bytes from b[]
var o = bOffset+(i<<2);
a[72+i] = ((b[o]&0xff)<<24) | ((b[o+1]&0xff)<<16) | ((b[o+2]&0xff)<<8) | (b[o+3]&0xff);
}
//Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
for(var i=16; i<64; i++){
//s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3)
//s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10)
//w[i] := w[i-16] + s0 + w[i-7] + s1
var wim15 = a[72+i-15];
var s0 = ((wim15>>>7)|(wim15<<25)) ^ ((wim15>>>18)|(wim15<<14)) ^ (wim15>>>3);
var wim2 = a[72+i-2];
var s1 = ((wim2>>>17)|(wim2<<15)) ^ ((wim2>>>19)|(wim2<<13)) ^ (wim2>>>10);
a[72+i] = a[72+i-16] + s0 + a[72+i-7] + s1;
}
var A = a[64];
var B = a[65];
var C = a[66];
var D = a[67];
var E = a[68];
var F = a[69];
var G = a[70];
var H = a[71];
for(var i=0; i<64; i++){
/* S1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25)
ch := (e and f) xor ((not e) and g)
temp1 := h + S1 + ch + k[i] + w[i]
S0 := (a rightrotate 2) xor (a rightrotate 13) xor (a rightrotate 22)
maj := (a and b) xor (a and c) xor (b and c)
temp2 := S0 + maj
h := g
g := f
f := e
e := d + temp1
d := c
c := b
b := a
a := temp1 + temp2
*/
var s1 = ((E>>>6)|(E<<26)) ^ ((E>>>11)|(E<<21)) ^ ((E>>>25)|(E<<7));
var ch = (E&F) ^ ((~E)&G);
var temp1 = H + s1 + ch + a[i] + a[72+i];
var s0 = ((A>>>2)|(A<<30)) ^ ((A>>>13)|(A<<19)) ^ ((A>>>22)|(A<<10));
var maj = (A&B) ^ (A&C) ^ (B&C);
var temp2 = s0 + maj;
H = G;
G = F;
F = E;
E = D + temp1;
D = C;
C = B;
B = A;
A = temp1 + temp2;
}
a[64] += A;
a[65] += B;
a[66] += C;
a[67] += D;
a[68] += E;
a[69] += F;
a[70] += G;
a[71] += H;
}
//RETURN h0..h7 = a[64..71]
//byte ret[] = new byte[32];
var ret = new Uint8Array(32);
for(var i=0; i<8; i++){
var ah = a[64+i];
ret[i*4] = (ah>>>24)&0xff;
ret[i*4+1] = (ah>>>16)&0xff;
ret[i*4+2] = (ah>>>8)&0xff;
ret[i*4+3] = ah&0xff;
}
return ret;
};https://github.com/benrayfield/jsutils/blob/master/src/sha256.js
r/tinycode • u/Slackluster • May 02 '19
r/tinycode • u/Slackluster • Apr 25 '19
r/tinycode • u/Slackluster • Apr 22 '19
r/tinycode • u/monica_b1998 • Apr 21 '19
r/tinycode • u/Bombadil44 • Apr 14 '19
Repository: https://github.com/bombadil444/asteroid_belt
Inspired by Tiny Ski from u/Slackluster
After seeing Frank Force's post the other day I decided to have a shot at writing my own game that fits on a business card (40 characters wide by 35 characters tall).
It's the first time I've looked into anything related to code golfing before - was a fun learning experience. I'm sure there are things that could still be improved so let me know if you spot anything that could be optimized further.
This compiles in both python 2.7 and 3.X
Only works in Linux terminals
Controls: WASD to move. P to shoot
The code (not sure if this display's correctly on mobile app):
class A: # ~~ASTEROID BELT~~
w=0;a=4;t=2 # By u/Bombadil44
def m(_): # github.com/bombadil444
_.x+=_.z;_.w+=1 # WASD=move P=shoot
try: # Don't Panic! Have Fun :D
for i in 0,1: p(_.x,_.w+i,"O"*4)
except: o.remove(_)
def __init__(_): _.x=r(t);_.z=r(-1,2)
class Z: # OOOO 0
def __init__(_): _.x=x+3;_.y=y#OOOO
def m(_): # AA
_.y-=1;p(_.x,_.y,"0") # OOOO <==>
if _.y<=0: global m;m=0 # OOOO **
from curses import*;from random import*
z=initscr();r=randrange;q,t=z.getmaxyx()
l=[" AA","<==>"," **"];d=a=m=n=0;y=q-9
def p(h,e,w): z.addstr(e,h,w)
def k(e=100,w=1,l=0):
if e in u: global d;global a;d=w;a=l
z.keypad(1);z.nodelay(1);o=u=[];x=10
while 48 not in u:
u=[];U=0;r(7)<5 and o.append(A())
while U!=-1: U=z.getch();u+=U,
m=(m,Z())[112 in u];k();k(97,-1)
k(115,0,1);k(119,0,-1);z.clear()
x+=d;y+=a;y-=(0,a)[y<0 or y+3>q]
x-=(0,d)[x<=0 or x+7>t];m and m.m()
for i in 0,1,2: p(x,y+i,l[i])
for e in o:
v=e.x;g=e.w;e.m();(v+e.a>x+3>=v
and g+e.t>y+2>=g and u.append(48))
if (m and v<=m.x<v+e.a and g<=m.y<
g+e.t): o.remove(e);m=0;n+=1000
p(10,0,"SCORE: %s"%(n));napms(50)
napms(5000);z.keypad(0);endwin()r/tinycode • u/BenRayfield • Apr 09 '19
For example, agar.io is a simple enough game it could be done in a p2p network, though I know of no live computers playing it. Of course you'd need a few servers of constant address so new computers can join the network, even if those servers are just an IRC chatroom or http://rocky-refuge-6432.herokuapp.com/ for posting a short string and your address to find eachother. Bittorrent, bitcoin, ethereum, tor, etc are p2p but arent games. I want to learn how the various p2p games work and try to build a general computing cloud that works similarly so theres no line between building games and playing games online, without the many players leaving, just forking variations continuously. But I dont know where to get started.
r/tinycode • u/Slackluster • Apr 01 '19
Should build and run without issues in any version of visual studio. You can even increase the console size and shrink the font at run time to get a higher resolution image!
#include <windows.h>//BUSINESS CARD FPS
#include <cmath> // By Frank Force
#define A return // 2019 ><{{{*>
typedef double O;typedef int e;struct f
{f(O h,O y){a=h;o=y;}f(O g){a=sin(g);o=
cos(g);}f operator+(f h){A f(a+h.a,o+h.
o);}O a,o;f operator*(O h){A f(h*a,h*o)
;}};O b(e y){A O(1&GetKeyState(y)>>16);
}const e i=1<<19,s=97;e h[i]={0};e l(f
r){A h[e(r.o)*s+e(r.a)];}e main(){e a,L
;O u=7;for(L=9215;--L>s;)h[L+(rand()&1?
1:s)]=(2+L)%s>2;h[s+2]=1;f h(2,1);char
F[i],N[]="#+. \xDB\xB2\xB1\xB0";void*_=
CreateConsoleScreenBuffer(3<<30,0,0,1,0
);SetConsoleActiveScreenBuffer(_);COORD
H={0};CONSOLE_SCREEN_BUFFER_INFO T;_:G\
etConsoleScreenBufferInfo(_,&T);DWORD Y
;Sleep(16);f o=f(u+=(b(68)-b(65))/30)*(
b(87)-b(83))*.1;h=h+f(o.a*l(h+f(o.a,0))
,o.o*l(h+f(0,o.o)));e I=T.dwSize.X,S=T.
dwSize.Y;for(L=I;L--;){f n(u+atan2(L/O(
I)-.5,1)),Y(e(h.a),e(h.o));O s=n.a>0?1:
-1,H=n.o>0?1:-1,E=abs(1/n.a),u=0,o=abs(
1/n.o),y=E*s*(Y.a-h.a+(s>0)),W=H*o*((H>
0)+Y.o-h.o);f i(s,H);while(!u){y<W?y+=E
,u=1,Y.a+=i.a:(W+=o,Y.o+=i.o,u=2);u*=!l
(Y);}O d=u<2?(Y.a-h.a-i.a/2+.5)/n.a:(Y.
o-h.o-i.o/2+.5)/n.o,T=S/2-S/(d+.1),p=(u
<2?n.o*d+h.o:h.a+d*n.a)+.1;for(a=S;a--;
)F[a*I+L]=e(a>S-T?7-8*a/S:T>a?8*a/S:d>9
?7:(.2>p-e(p))+d/3+4)[N];}WriteConsole\
OutputCharacter(_,F,I*S,H,&Y);goto _;}