Misc. Code
Copy this into the Julia REPL to watch the π© spin!
function donut()
A=B=1;b=Vector(undef,1760)
z=Vector(undef,1760);println(
"\33[2J");print("\e[?25l");while true
A+=0.07;B+=0.03;cA=cos(A);sA=sin(A);cB=
cos(B);sB=sin(B);for k = 1:length(b);b[k]=
k%80==79 ? "\n" : " ";z[k] = 0;end;for j in
range(0, step= 0.07,stop = 6.28);
ct=cos(j);st= sin(j); for i in
range(0,step= 0.02, stop=6.28);
sp=sin(i); cp= cos(i);h=ct+2;D=1/
(sp*h*sA+st*cA+ 5);t=sp*h*cA-st*sA
x=0|trunc(Int,40+ 30*D*(cp*h*cB-t*sB))
y=0|trunc(Int,12+15*D*(cp*h*sB+t*cB));o=x+
80*y;N=0|trunc(Int, 8*((st*sA-sp*ct*cA)*
cB-sp*ct*sA-st*cA-cp*ct*sB));if y<22&&
y>=0&&x>=0&&x<79&&D>z[o];z[o]=D;b[o]=
".,-~:;=!*#\$@"[N>1 ? N : 1];end
end;end;println("\033[1;1H"*
join(b));sleep(0.03)
end;end;donut()
REPL Games
I wrote Tetris in one functional line of Julia codeβnot using semicolons to break up separate Julia lines. Instead, I'm using tricks to define one tetris(...)
function that encodes the game state variables and subroutines as function arguments and then runs the game loop when the tetris
function is executed.
It works cross-platform in pure Julia as I did not want to rely on any external libraries. Games are limited to 1 hour (72,000 iterations with a game clock of 0.05 seconds), as while loops would break my "don't use semicolons to separate lines" rule (aside from matrix row definitions). It's added to the Julia package registry so run using Tetris
to play (or copy and paste this block into the Julia REPL and play).
module Tetris begin global (pd=false, buf=Channel{Char}(0x737415), sc=0, hs=0, S=" "^2, rf=(ls=sc, bs=hs) -> ["β" "ββββ" "βββ" "T" "E" "T" "R" "I" "S" "ββββ" "βββ" "β"; "β" S S S S S S S S S S "β βββNEXTβββ"; "β" S S S S S S S S S S "β β$S$S$S$(S)β"; "β" S S S S S S S S S S "β β$S$S$S$(S)β"; "β" S S S S S S S S S S "β β$S$S$S$(S)β"; "β" S S S S S S S S S S "β β$S$S$S$(S)β"; "β" S S S S S S S S S S "β ββββββββββ"; "β" S S S S S S S S S S "β$(S)Score: $ls"; "β" S S S S S S S S S S "β$(S) High: $bs"; "β" S S S S S S S S S S "β";"β" S S S S S S S S S S "β";"β" S S S S S S S S S S "β"; "β" S S S S S S S S S S "β";"β" S S S S S S S S S S "β";"β" S S S S S S S S S S "β"; "β" S S S S S S S S S S "β";"β" S S S S S S S S S S "β";"β" S S S S S S S S S S "β"; "β" "ββ" "ββ" "ββ" "ββ" "ββ" "ββ" "ββ" "ββ" "ββ" "ββ" "β"], f=rf(), b=nothing, bi=rand(1:7), nbi=rand(1:7), fw=12, fh=18, sX=div(fw,2)-1, sY=2, X=sX, Y=sY, alv=false, play(ββ="ββ", β β ="[]", R="\e[31m$ββ\e[0m", B="\e[31;1m$ββ\e[0m", G="\e[32m$ββ\e[0m", L="\e[33m$ββ\e[0m", N="\e[34m$ββ\e[0m", M="\e[35m$ββ\e[0m", C="\e[36m$ββ\e[0m", W="\e[37m$ββ\e[0m", bs=[[S S C S; S S C S; S S C S; S S C S], [S B S S; S B S S; S B B S; S S S S], [S S N S; S S N S; S N N S; S S S S], [S S S S; S L L S; S L L S; S S S S], [S G S S; S G G S; S S G S; S S S S], [S S R S; S R R S; S R S S; S S S S], [S S M S; S M M S; S S M S; S S S S]], nb=bs[nbi], rb=nothing, fd=false, esc=false, rtx=false, h=false, p=(X,Y), rcan=false, lr=[], fwlr=nothing, key=nothing, t=0, sp=10, af=copy(f), dn= (lf, lnb) -> [lf[y,end] = replace(lf[y,end], r" β.{8,100}β"=>" β"*join(lnb[i,:])*"β") for (i,y) in enumerate(3:6)], c=(lb, lf, xy, rt::Bool=false) -> ([rt ? nothing : (rt = (lb[j,i] != S && lf[y,max(1,x)] != S)) for (i,x) in enumerate(xy[1]:xy[1]+3) for (j,y) in enumerate(xy[2]:xy[2]+3)], rt)[end], gd=(lb, lf, lX, ly, lp, rt::Bool=false) -> ([rt ? nothing : (!c(lb, lf, lp) ? (()->lp=(lX,py))() : rt=true) for py in ly:fh], (()->ly=lp[2]-1)(), (lp, ly))[end], sh=(lb) -> map(s->replace(s,ββ=>β β ), lb), m=(lb, lf, xy, nf=deepcopy(lf)) -> ([lb[j,i] != S ? (nf[y,x] = lb[j,i]) : nothing for (i,x) in enumerate(xy[1]:xy[1]+3) for (j,y) in enumerate(xy[2]:xy[2]+3)], nf)[end], ds=(lf, lb, laf, lX, ly, lp, sb=sh(lb), np=gd(sb,lf,lX,ly,lp)[1]) -> m(lb, m(sb, laf, (np[1], np[2]-1)), lp), df=(lf, lb, lX, ly, lp, laf=lf, naf=(!pd && !isempty(lp) ? ds(lf, lb, laf, lX, ly, lp) : laf)) -> println("\033[1;1H",join(join.([naf[i,:] for i in 1:size(naf,1)]), "\n"), "\033[$(fh+1);$(fw+1)H", "\n Hit ` to pause ", "\033[$(fh+1);$(fw+1)H"), cr=(lrb, lp::Tuple, rt::Tuple=(false,lp)) -> ([!c(lrb, f, (lp[1]+r,lp[2])) ? (r == 0 ? rt=(true, lp) : rt=(rt[1] ? rt : (true,(lp[1]+r, lp[2])))) : nothing for (i,r) in enumerate([0,-1,1,-2,2])], rt)[end], cl=(lf) -> [all(map(line->!occursin(S,line), lf[i,2:end-1])) for i in 2:size(lf,1)-1], cc=() -> ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid}, Int32), stdin.handle, true), kbr=() -> ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid}, Int32), stdin.handle, false), asy=begin @async begin for __ in 1:72000 pd ? break : put!(buf, read(stdin, Char)) end end end, ri=() -> (isready(buf) ? take!(buf) : nothing), fls=() -> try Base.throwto(asy, InterruptException()) catch end, shc=() -> println("\e[?25h"), ps=() -> (println("ββββββββββββββββββββββ\n", "β Tetris.start() β\n", "ββββββββββββββββββββββ"), kbr(), shc(), fls(), global pd=true)) = (begin global pd=false end, begin global X=X end, begin global Y=Y end, begin global b=isnothing(b) ? bs[bi] : b end, cc(), println("\33[2J"), print("\e[?25l"), (()->f=rf())(), dn(f, nb), [(sleep(0.05), (()->t+=1)(), (()->fd=(t==sp))(), (()->p=(X,Y))(), (!isempty(lr) ? (df(f,b,X,Y,p), begin global sc+=length(lr) end, begin global hs=max(sc,hs) end, (()->fwlr=f[setdiff(2:size(f,1)-1,lr),2:end-1])(), (()->f=rf(sc))(), (()->f[(2+length(lr)):end-1, 2:end-1]=fwlr)(), dn(f, nb), sleep(0.05), (()->lr=[])(), df(f,b,X,Y,p)) : nothing), (()->key=ri())(), begin global alv=alv ? true : !isnothing(key) end, ((key=='a') ? ((!rtx ? (()->p=(X-1,Y))() : nothing), (()->rtx=true)()) : ((key=='d') ? ((!rtx ? (()->p=(X+1,Y))() : nothing), (()->rtx=true)()) : ((key=='s') ? (()->p=(X,Y+1))() : ((key=='w') ? ((!h ? ((()->(p,Y)=gd(b,f,X,Y,p))(), (()->fd=true)()) : nothing), (()->h=true)()) : (((key=='q') || (key=='e')) ? (((()->rb=((key=='q') ? rotl90(b) : rotr90(b)))(), (!h ? (()->((()->(rcan,p)=cr(rb,p))(), (rcan ? (begin global b=rb end) : nothing)))() : nothing), (()->h=true)())) : ((key=='`') ? ((()->esc=true)()) : (()->h=false)())))))), rtx ? (sleep(0.025),(()->rtx=false)()) : nothing, !c(b,f,p) ? (begin global X=p[1] end, begin Y=p[2] end) : begin p=(X,Y) end, (()->af=m(b,f,p))(), fd ? (!c(b, f, (p[1], p[2]+1)) ? (()->global Y+=1)() : ((()->f[:]=af)(), (any(cl(f)) ? ((l=findall(cl(f)).+1)-> (f[l,2:end-1] .= W, (()->lr=l)()))() : (()->lr=[])()), ((()->global X=sX)(), (()->global Y=sY)(), (()->p=(X,Y))(), begin global bi=nbi end, begin b=nb end, begin global nbi=rand(1:7) end, begin nb=bs[nbi] end, dn(f, nb), (()->global pd=c(b,f,p))())), (()->t=0)()) : df(f,b,X,Y,p,af), pd && (fls(), begin global sc=0 end, begin global f=rf() end, alv ? play() : ps()), esc && ps()) for _ in 1:72000 if !pd]), start() = (print("\e[?25l"), play(), nothing)[end], __init__() = play()) end end
Research Code
Teaching Code
LaTeX Code
Code Golf
Code golf profile: https://code.golf/golfers/mossr
One-line fizz-buzz
Print the numbers from 1 to 100 inclusive, each on their own line. If, however, the number is a multiple of three then print Fizz instead, and if the number is a multiple of five then print Buzz. If multiple conditions hold true then all replacements should be printed, for example 15 should print FizzBuzz.
map((i,f=i%3==0,b=i%5==0)->println("$i"^(1-(f|b))*"Fizz"^f*"Buzz"^b),1:100)
One-line Fibonacci sequence
The Fibonacci numbers are a numerical sequence in which each number is the sum of the two preceding numbers: 0, 1, 1, 2, 3, 5, 8, 13.... Print the first 31 Fibonacci numbers from Fβ = 0 to Fββ = 832040 (inclusive), each on a separate line.
F(n)=n<2 ? n : F(n-1)+F(n-2);[println(F(n)) for n=0:30]
Leap years
In the Gregorian calendar, a leap year is created by extending February to 29 days in order to keep the calendar year synchronized with the astronomical year. These longer years occur in years which are multiples of 4, with the exception of centennial years that aren't multiples of 400. Write a program to print all the leap years from the year 1800 up to and including 2400.
t=100;Y=18t:4:24t;@.println(Y[!(Y%4tβ Y%tβ‘0)])
Divisors
A number is a divisor of another number if it can divide into it with no remainder. Print the positive divisors of each number from 1 to 100 inclusive, on their own line, with each divisor separated by a space.
for i=1:100;X=1:i;println(join(X[i.%X.==0]," "))end
Evil numbers
An evil number is a non-negative number that has an even number of 1s in its binary expansion. Print all the evil numbers from 0 to 50 inclusive, each on their own line.
X=0:50;println.(X[[sum(i.>>(0:5).&1)%2==0 for i=X]])
Diamonds
Draw a size ascending range of diamonds using the numbers 1 to 9, ranging from size 1 to size 9, each diamond separated by a blank line. A size 1 diamond should look like this, a single centered 1:
1
With the largest size 9 diamond looking like this:
β¨ 1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
1234321
12321
121
1
for N=1:9,C=[1:N,N-1:-1:1],i=C;println(" "^(10-i),1:i...,i-1:-1:1...,"\n"^(iβ‘C[end]β‘1))end
Rock-paper-scissors-Spock-lizard
Judge games of "rock-paper-scissors" with additional weapons: β cuts π covers π crushes π¦ poisons π smashes β decapitates π¦ eats π disproves π vaporizes π crushes β. Each argument is a string of two emoji encoded in UTF-8. For each argument, print a line like π¦ poisons π describing the game outcome, or Tie if they are equal.
d=Dict("βπ"=>:cuts,"ππ"=>:covers,"ππ¦"=>:crushes,"π¦π"=>:poisons,"πβ"=>:smashes,"βπ¦"=>:decapitates,"π¦π"=>:eats,"ππ"=>:disproves,"ππ"=>:vaporizes,"πβ"=>:crushes)
map((a,v=reverse,t=collect,h=haskey,p=println,c=t(a),r=v(c),z=v(a),y=t(z))->h(d,a) ? p(c[1]," ",d[a]," ",c[2]) : h(d,z) ? p(y[1]," ",d[z]," ",y[2]) : p(:Tie),ARGS)