r/codegolf • • Feb 19 '25

Starpath is 55 bytes

Thumbnail
hellmood.111mb.de
6 Upvotes

r/codegolf • • Jan 04 '25

Vi-like text editor for Linux (2000 bytes)

6 Upvotes
#!/bin/python3
import curses as u;s=u.initscr();s.nodelay(1);u.noecho();u.raw();s.keypad(1);v={'b':[],
'r':0,'c':0,'i':0,'m':'N','x':0,'y':0,'R':0,'C':0,'n':' ','f':'o.txt','u':u,'s':s,104:
'if c>0:c-=1',108:'if c<len(b[r]):c+=1',107:'if r!=0:r-=1',106:'if r<len(b)-1:r+=1',
100:'if len(b):del b[r];r=r if r<len(b) else r-1 if r-1 >= 0 else 0',36:'c=len(b[r])',
48:'c=0',21:'r=r-5 if r-5>0 else 0',4:'r=r+5 if r+5<len(b)-1 else len(b)-1',105:'m="I"',
120:'if len(b[r]):del b[r][c]\nif c and c>len(b[r])-1:c=len(b[r])-1','t':['if i!=((i)&',
'0x1f) and i<128:b[r].insert(c,i);c+=1\nif i==263:\n if c==0 and r!=0:l=b[r][c:];del ',
'b[r];r-=1;c=len(b[r]);b[r]+=l\n elif c:c-=1;del b[r][c]\nif i==10:l=b[r][c:];b[r]=',
'b[r][:c];r+=1;c=0;b.insert(r,[]+l)'],'p':['R,C=s.getmaxyx();R-=1\nif r<y:y=r\nif ',
'r>=y+R:y=r-R+1\nif c<x:x=c\nif c>=x+C:x=c-C+1\nfor Y in range(R):\n for X in range(C):',
'\n try:s.addch(Y,X,b[Y+y][X+x])\n except:pass\n s.clrtoeol()\n try:s.addch(10)\n ',
'except:pass\nu.curs_set(0);s.clrtoeol();s.addstr(R,0,f+n+str(r)+":"+str(c));',
's.move(r-y,c-x);u.curs_set(1);s.refresh();i=-1'],'a':['if not len(b):b=[[]]\nif c>len(',
'b[r]):c=len(b[r])'],'z':['try:\n with open(f) as i:\n c=i.read().split("\\n");c=c[:-1] ',
'if len(c)>1 else c\n for i in c:b.append([ord(c) for c in i]);r=len(b)-1;c=len(b[r])',
'\nexcept:b.append([])'],'w':['d=""\nfor l in b:d+="".join([chr(c) for c in l])+"\\n"\n',
'with open(f,"w") as i:i.write(d);n=" "']};exec(''.join(['import sys\ndef w(n):',
'exec("".join(v["w"]),v)\ndef r(n):exec("".join(v["z"]),v)\nif len(sys.argv)==2:',
'v["f"]=sys.argv[1];r(sys.argv[1])\nif len(sys.argv)==1:v["b"].append([])\nwhile ',
'True:\n try:\n exec("".join(v["p"]),v)\n while (v["i"]==-1):v["i"]=s.getch()\n ',
'v["n"]="*"\n if v["i"]==17:break\n if v["i"]==27:v["m"]="N"\n if v["i"]==23:',
'w(v["f"])\n if v["m"]=="N":exec(v[v["i"]],v)\n elif v["m"]=="I":exec("".join(',
'v["t"]),v)\n exec("".join(v["a"]),v)\n except:pass']),{'v':v,'s':s});u.endwin()

GitHub project: https://github.com/maksimKorzh/e


r/codegolf • • Aug 29 '24

Radix sort in JS, 75 bytes.

6 Upvotes

A function that sorts an array of positive integers using radix sort with radix = 2. My first version was 112 bytes long, then I shortened it to 84 bytes:

l=>{for(b=1;b<<=1;)for(i in k=0,l)l[i]&b||l.splice(k++,0,l.splice(i,1)[0]);return l}

Later it was shortened to 81 bytes by a guy from a chat (he added recursion to remove for and return):

l=>(f=b=>b?f(b<<=1,k=0,l.map((x,i)=>x&b||l.splice(k++,0,l.splice(i,1)[0]))):l)(1)

Then I shortened the 84 version to 75 bytes, however, this version does not return the array, but modifies the source array:

l=>{for(b=1;k=0,b<<=1;)l.map((x,i)=>x&b||l.splice(k++,0,...l.splice(i,1)))}


r/codegolf • • Jan 02 '24

The 40 day countdown to the Lovebyte sizecoding/codegolfing demoparty ( https://lovebyte.party ) on February 9-11th 2024, starts now!

Thumbnail
youtube.com
6 Upvotes

r/codegolf • • Oct 06 '22

JS 13K 2022 winners 🏆

Thumbnail
github.blog
6 Upvotes

r/codegolf • • Aug 18 '22

Levy Dragon Animation in 32 bytes (x86)

Thumbnail
self.tinycode
6 Upvotes

r/codegolf • • Jul 27 '22

Patrick Bateman's fractal flame business card

6 Upvotes
#include <stdlib.h> //gcc -o card card.c -lm	
#include <math.h>	//./card > card.ppm
#define Z printf(
typedef float f;typedef int d;d R=600;d C=1050;
f O=1.;f m=1.;f r(){return (f)rand()/RAND_MAX;}
f z(f x,f o){return fmod(fmod(x,o)+o,o);}f t=
1./22.;f p=0.04;f N=0.;f H=0.5;f V=0.25;void c(f 
(*q)[C*3][4]){f e=0.065;f b=0.14;f I[25][10]={{N,
-b,b,N,-3.3,O,t},{N,-b,p,N,-2.2,1.54,t},{e,N,N,b,
-3.,2.,t},{e,N,N,b,-3.,O,t},{p,-b,b,p,-m,O,t},{p,
b,-b,p,-0.75,1.6,t},{.039,N,N,b,-O,m,t},{b,N,N,b,
0.75,2.,t},{N,-b,0.104,N,m,O,t},{N,-b,b,N,2.7,O,t
},{N,-b,p,N,3.8,1.54,t},{e,N,N,b,3.,2.,t},{e,N,N,
b,3.,O,t},{0.079,0.07,-p,b,3.,0.7,t},{N,-b,0.104,
N,5.,0.8,t},{e,N,N,b,6.75,2.,t},{e,N,N,b,6.75,0.4
,t},{N,-b,b,N,6.5,O,t},{N,-b,b,N,8.5,O,t},{e,-0.1
,e,0.1,9.,1.4,t},{e,0.1,-e,0.1,9.,O,t}};
f J[25][10]={{N,-b,b,N,-3.3,O,p},{e,N,N,b,-3.,O,p}
,{e,N,N,b,-3.,V,p},{N,-b,p,N,-2.2,H,p},{p,-b,b,p,-
m,O,p},{p,b,-b,p,-0.75,1.6,p},{p,N,N,b,-O,m,p},{b,
N,N,b,0.75,2.,p},{N,-b,0.104,N,m,O,p},{e,N,N,b,3.,
2.,p},{e,N,N,b,3.,0.4,p},{N,-b,b,N,2.75,O,p},{p,N,
N,b,3.,1.2,p},{N,-b,b,N,4.4,O,p},{N,-b,b,N,5.6,O,p
},{0.022,-b,p,p,5.4,1.52,p},{0.031,b,-0.086,p,4.5,
1.76,p},{p,-b,b,p,6.75,O,p},{p,b,-b,p,7.25,1.6,p},
{p,N,N,b,7.,m,p},{N,-b,b,N,8.5,O,p},{N,-b,b,N,9.4,
O,p},{0.03,b,-0.1,p,8.5,1.7,p}};f D[4][3]={{m,0.8,
V},{m,H,0.44},{0.76,0.33,0.61},{H,0.33,0.62}};
f t=r();f s=r();f w[3]={r(),r(),r()};d i=0;d S=22;
f Y=0.4;f X=-b;f L=R*C;while(i<2*L){if(i==L){
memmove(&I,&J,sizeof(J));S=25;Y=-0.1;}f a=r();f P=
N;for(d z=0;z<S;z++){P=P+I[z][6];if(a<P){f x_t=t*
I[z][0]+s*I[z][1]+I[z][4];s=t*I[z][2]+s*I[z][3]+
I[z][5];t=x_t;w[0]=(w[0]+D[z%4][0])*H;w[1]=(w[1]+
D[z%4][1])*H;w[2]=(w[2]+D[z%4][2])*H;break;}}d W=
(d)(z((O-s*0.1+Y),O)*R*3);d B=(d)(z(t*0.1*R/C+H+X,
O)*C*3);q[W][B][0]=w[0];q[W][B][1]=w[1];q[W][B][2]
=w[2];q[W][B][3]+=1;if(q[W][B][3]>m){m=q[W][B][3];}
i++;}}d main(){Z"P6 %i %i 255 ",C,R);f (*q)[C*3][4];
q=malloc(R*144*C);c(q);for(d x=1;x<R*3-1;x+=3){for(
d y=1;y<C*3-1;y+=3){ f r=O;f g=O;f b=O;f U=O;for(d
 i=-1;i<2;i++){for(d j=-1;j<2;j++){d G=x+i;d F=y+j;
r+=q[G][F][0];g+=q[G][F][1];b+=q[G][F][2];U+=
q[G][F][3];}}f E=pow(log(U/9.)/log(m),1./2.2)*28.3;
Z"%c%c%c",(d)(r*E),(d)(g*E),(d)(b*E));}}}

r/codegolf • • Apr 20 '22

"One" - Procedural planet with music in 256 bytes - 1st @ "Revision 2022"

Thumbnail
pouet.net
6 Upvotes

r/codegolf • • Jan 05 '21

Today's prompt for Genuary (a month of generative art challenges) is 'code golf'. I'm a beginner to your crazy world, but how do you like my 300-character generative music Supercollider homage to Brian Eno?

Thumbnail
soundcloud.com
6 Upvotes

r/codegolf • • Oct 16 '20

Prime numbers in 63 bytes of ruby

6 Upvotes

s=[n=2];ARGV[0].to_i.times{n+=1until s.all?{|m|n%m>0};s<<p(n)}


r/codegolf • • Aug 15 '20

JS13K competition just started: build a JavaScript game in ≤ 13kB

Thumbnail
js13kgames.com
6 Upvotes

r/codegolf • • Dec 22 '19

Tic-Tac-Toe Challenge

6 Upvotes

So this has been done before on other sites like stackoverflow but I'm curious if anyone can find even sorter solutions.

This is the challenge: write a function which given an array of 9 integers (0 representing "empty board slot", 1 representing 'X', and 2 representing 'O') return the following values:

0 if no one has won, or the board is empty  
1 if X has won  
2 if O has won

So the code has to be in the form of a function (doesn't matter function name as long as it accepts an array for the board). Unlike some of the other requirements I don't care how many other paramters the func accepts, whether have a recursive solution, etc, just as long as its a function and it accepts at least one input array for the board.

This is my first attempt coming in at 107 chars of JS:

function t(b){
	i=9;r=0;
	while((!r)&&i--)r=b['01203602'[i]*1]&b['34514744'[i]*1]&b['67825886'[i]*1];
	return r;
}

Probs will try to make a shorter version again a little later if I have more time to fool around with this and will post back if I do.

Let's see who's got the shortest solutions!


r/codegolf • • Aug 07 '19

Build a game in ≤ 13kB of JavaScript with JS13KGames

Thumbnail
2019.js13kgames.com
6 Upvotes

r/codegolf • • Jul 17 '19

my first try at this: fizzbuzz! (91 characters)

6 Upvotes

function fb(n){return n==0?"":fb(n-1)+(n%15==0?"fizzbuzz":n%3==0?"fizz":n%5==0?"buzz":n);}


r/codegolf • • Jun 18 '19

GitHub's Code Golf Challenge - Golfbot

Thumbnail
noopschallenge.com
6 Upvotes

r/codegolf • • Apr 25 '19

Sierpinski Triangles in C, 104 bytes

Thumbnail
github.com
6 Upvotes

r/codegolf • • Apr 12 '19

My C++ homework in 260 characters

6 Upvotes
#define q int const
bool sameSet(q*a,q*b,q*c,q*d){if(b-a>d-c)swap(a,c),swap(b,d);int s=1,r=0;for(;a!=b;s*=*a++);for(;c!=d;r|=s%*c++);return!r;}void copyEvens(q a[],size_t c,int b[],size_t&d){d<c?throw length_error(0):d=0;for(q*e=a+c;a<e;a++)if(~*a%2)b[d++]=*a;}

r/codegolf • • Jan 02 '19

Something to warm the cold days in front of your terminal (279 chars bash)

6 Upvotes
t()(tput $@);for((;;));{ t cup 0 0;l=();x=$((\`t cols\`-1));for((r=0;r<=$((\`t lines\`-1));r++));{ for((c=0;c<=$x;c++));{ ci=$\[c+RANDOM%3-1\];cl=$\[${l\[ci>x?x:(ci>=0?ci:0)\]:-0}+RANDOM%2\];l\[c\]=$cl;cv=$\[cl>10?0:cl>5?160-(cl-6)\*36:226-cl\*6\];echo -ne "\\e\[48;5;${cv}m \\e\[0m";};};sleep 0.1;}

r/codegolf • • Dec 05 '18

Find what a phrase with removed letters originally said in fewest characters

6 Upvotes

There are two inputs: one string of A-Z capital letters that is a phrase with certain letters removed, as well as spaces and punctuation. Example: "THSSANEAMPLE", and another string of the letters that have been removed. Example: "IX".

The program will then output all possible phrases that could have been written originally, using a generally reputable source of most words in the english language such as the Oxford English dictionary.

  • You are allowed to access any website you want, as long as it was created before this question was originally asked.

  • You are allowed to not output any phrases that are as long as twice the original string.

  • Slang or names of places or people are not required.

  • Disregard whether a phrase would be syntactically or grammatically correct.

  • The program is allowed to crash if there is no possible phrase, or if anything besides capital letters are in either string


r/codegolf • • Oct 05 '18

A Very Small SAT Solver

6 Upvotes

Not my own work, but I thought it would be appreciated here:

http://www.cse.chalmers.se/~algehed/blogpostsHTML/SAT.html


r/codegolf • • Mar 06 '18

I know the point of codegolf is to be fun/challenge, but what are practical applications of codegolfing?

6 Upvotes

Have any of you used it in practice sometime, or know of some interesting use cases?


r/codegolf • • Jul 27 '17

Browser psychedelica in 135 characters

6 Upvotes
setInterval("document.querySelectorAll('*').forEach(a=>a.style.background='#'+(Math.random()*16777215).toString(16).substr(0,6))",300)

r/codegolf • • Feb 12 '17

Here's an archive of 140byt.es entries - JavaScript snippets that fits into tweet.

Thumbnail aishikaty.github.io
6 Upvotes

r/codegolf • • Aug 06 '16

[js] return true to win

Thumbnail alf.nu
6 Upvotes