Coding Challenge #2! (115)

1 Name: Yaranaika Daddy!K./5Izeg0I 05/01/08(Sat)11:21 ID:Heaven This thread was merged from the former /code/ board. You can view the archive here.

Alright people who know how to tell there computer what to do and how to get there... I bring you a challenge from the interweb.

THE 4K CODE CHALLENGE

Objective: To create the most productive/useful code that as source code is no larger than 4 kilobytes (4096 bytes). There is no restrictions as to what your code does, or what language it is used in.

Rules:

  • The code must be yours, and you must be willing to give your code out to the public domain.
  • You must state EXACTLY what your code does. If it is malicious you must state what it does/exploit.
  • There is no restriction on what language, however markup languages are not allowed. Javascript/VBScript are, and any HTML required to perform the task is not counted towards the byte tally however objects calling to Javascript/VBScript do.
  • Common standard libraries (.h files, javabeans, perl modules etc) are allowed and dont count towards the byte tally.
  • You can use as many seperate files in your source code as you wish.
  • If you code requires to be compiled to run, it must be able to compile without a problem. For C and other languages that require a makefile, these will not go towards the byte tally.
  • Any other data in the form of databases, information etc that isn't a part of the code also does not count towards the byte tally.
  • If your program requires parameters to begin, you must state what they do.

Gentlemen, START YOUR TEXTEDITORS

101 Name: #!/usr/bin/anonymous : 2007-10-21 14:37 ID:Heaven

That's great. Carpet fishing.

102 Name: #!/usr/bin/anonymous : 2007-10-22 16:15 ID:YCkegZ+g

>>100
Which is very unfortunate. I haven't been able to get my ``EXPERT BBCODE'' fix for a few days.

103 Name: #!/usr/bin/anonymous : 2008-04-20 01:57 ID:Ly9pCEoR

104 Name: #!/usr/bin/anonymous : 2008-04-20 19:08 ID:OOD/GMZb

The averaging solution to Laplace's equation. More specifically, it's a 1000V and a -1000V wire near each other in a conducting pipe and I find the voltage in the pipe. While ago though, I've to look it up to see if that's right. Used symmetry to speed it up. You can change the shape and the wire's voltage and get rid of the symmetry. Graphs the result. It's in Matlab. Am I cool yet?

``
m=201;
n=201;

a = 0.5;
x = 0:1/200:a;
y = 0:1/200:a;

s = zeros(m,n);

[u1,u2] = meshgrid(y.^2,x.^2); % shape
out = find(u1+u2 >= a^2); % points outside the circle

e = 1;
while e<1000,

e = e + 1;
s = ([s(2,:) ; s(1:end-1,:)] ...
+ [s(2:end,:) ; zeros(1,n)] ...
+ [zeros(m,1) s(:,1:end-1)] ...
+ [s(:,2:end) zeros(m,1)])/4; % averaging
s(out) = 0; % outside pipe
s(1,5) = 10; % the 1000V wire
s(:,1) = 0; % y = 0

end

s1 = [flipud(s) ; s(2:end,:)];
s2 = [fliplr(-s1) s1(:,2:end)]; % symmetry across the y-axis
mesh(s2)
``

105 Name: #!/usr/bin/anonymous : 2008-04-23 18:26 ID:6rn5Nb8P

reposting this from /prog/ (hi guys!) to compete in the 4K challenge:

<html><head><title>CBC using Mersenne Twister</title><style>body { color: white; background-color: black; }</style><script type="text/javascript">
N = 624;
M = 397;
MATRIX_A = 0x9908b0df;
UPPER_MASK = 0x80000000;
LOWER_MASK = 0x7fffffff;
var mt = new Array(N);
var mti = N+1;
function unsigned32 (n1) { return n1 < 0 ? (n1 ^ UPPER_MASK) + UPPER_MASK : n1; }
function subtraction32 (n1, n2) { return n1 < n2 ? unsigned32((0x100000000 - (n2 - n1)) & 0xffffffff) : n1 - n2; }
function addition32 (n1, n2) { return unsigned32((n1 + n2) & 0xffffffff) }
function multiplication32 (n1, n2) {var sum = 0;for (var i = 0; i < 32; ++i){if ((n1 >>> i) & 0x1){sum = addition32(sum, unsigned32(n2 << i));}}return sum;}
function init_genrand(s){mt[0]= unsigned32(s & 0xffffffff);for (mti=1; mti<N; mti++) {mt[mti] = addition32(multiplication32(1812433253, unsigned32(mt[mti-1] ^ (mt[mti-1] >>> 30))), mti);mt[mti] = unsigned32(mt[mti] & 0xffffffff);}}
function init_by_array(init_key, key_length){var i, j, k;init_genrand(19650218);i=1; j=0;k = (N>key_length ? N : key_length);for (; k; k--) {mt[i] = addition32(addition32(unsigned32(mt[i] ^ multiplication32(unsigned32(mt[i-1] ^ (mt[i-1] >>> 30)), 1664525)), init_key[j]), j);mt[i] = unsigned32(mt[i] & 0xffffffff);i++; j++; if (i>=N) { mt[0] = mt[N-1]; i=1; } if (j>=key_length) j=0; } for (k=N-1; k; k--) {mt[i] = subtraction32(unsigned32((dbg=mt[i]) ^ multiplication32(unsigned32(mt[i-1] ^ (mt[i-1] >>> 30)), 1566083941)), i);mt[i] = unsigned32(mt[i] & 0xffffffff);i++; if (i>=N) { mt[0] = mt[N-1]; i=1; } } mt[0] = 0x80000000; }
function genrand_int32() { var y; var mag01 = new Array(0x0, MATRIX_A); if (mti >= N) { var kk; if (mti == N+1) init_genrand(5489); for (kk=0;kk<N-M;kk++) { y = unsigned32((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)); mt[kk] = unsigned32(mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]); } for (;kk<N-1;kk++) { y = unsigned32((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)); mt[kk] = unsigned32(mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]); } y = unsigned32((mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK)); mt[N-1] = unsigned32(mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]); mti = 0; } y = mt[mti++]; y = unsigned32(y ^ (y >>> 11)); y = unsigned32(y ^ ((y << 7) & 0x9d2c5680)); y = unsigned32(y ^ ((y << 15) & 0xefc60000)); y = unsigned32(y ^ (y >>> 18)); return y; }
function l() { pass = document.getElementById("pass").value; if (pass) { init_by_array(pass,pass.length); return true; } else { alert('ENTER PASSWORD FIRST'); return false; } }
function dec2hex(i) { hex = "0123456789ABCDEF"; r = i % 16; s = hex.charAt((i-r)/16) + hex.charAt(r); return s; }
function e() { if (l()) { iv = Math.floor(Math.random()*256); body = document.getElementById("body").value; result = dec2hex(iv); for (i=0;i<body.length;i++) { if ((i+1)%30 == 0) result = result + "\n"; iv = (body.charCodeAt(i)+iv+genrand_int32())%256; result = result + dec2hex(iv); } document.getElementById("body").value = result; } }
function d() { if (l()) { r = /[^0-9A-F]/g; body = document.getElementById("body").value.replace(r,""); result = ""; for(i=1;(i*2)<body.length;i++) { s=(parseInt(body.substr(i*2,2),16)-parseInt(body.substr((i-1)*2,2),16)-genrand_int32()); if (s<0) { s=256-((-s)%256); } result = result + String.fromCharCode(s); } document.getElementById("body").value = result; } }
</script></head><body><form><pre>
Cipher Block Chaining using the Mersenne Twister (check wikipedia)

Password: <input class="inputText" type="password" value="anonIRCd" id="pass">

<textarea id="body" rows="20" cols="65"></textarea>

<input id="encrypt" type="button" value="Encrypt" onclick="e();"> <input id="decrypt" type="button" value="Decrypt" onclick="d();">
</pre></form></body></html>

what does anon think?

734DB4F1F24FC32D04E110418D7C99A74645CFDCFB1E5B035AE3EAACC821
96E71CD4A1EA902FC5EAB0DE2527C0E6954EF54839690166269E68C105DE
A6A9BD215CE6

106 Name: #!/usr/bin/anonymous : 2008-04-23 18:45 ID:Hnry8iXO

> what does anon think?
Unlike Blum Blum Shub, the algorithm in its native form is not suitable for cryptography.

PS: "anon" lives at 4chan. Here we are anonymous, not Anonymous.

107 Name: #!/usr/bin/anonymous : 2008-04-27 05:20 ID:xxFWOzRb

Monads in Javascript

// Monad stuff
function bindM(m ,k)
{
return function (s) {
tmp = m(s);
a = tmp[0]; s_ = tmp[1];
return k(a)(s_);
}
}
function thenM(m,k) { return bindM(m,function(_){return k;}); }
function returnM(v) { return function (s) { return [v,s]; } }
function evalS(m,s) { return m(s)[0]; }
function getS(s) { return [s,s]; }
function putS(s) { return function(_) { return [null,s]; } }
function doM(m) {
if (arguments.length == 1) return m;
else if (arguments.length % 2 && arguments.length >= 2) {
var rest = Array.prototype.slice.call(arguments).slice(2);
return arguments[1](m, doM.apply(this, rest));
}
else throw ("doM: Error: Arguments mismatch.");
}
function mapM(k,l) {
if (l.length > 0) return doM(
k(l[0])
, bindM
, function (item) { return doM(
mapM(k,l.slice(1))
, bindM
, function (rest) {
if (rest)
return returnM(item.concat(rest));
else
return returnM(item);
}
); }
);
else
return returnM(null);
}
// Parse stuff
function parse(parser, input) { return evalS(parser, input); }
function throwInt(msg,input) { 
var e = new Error();
e.message = [msg,input];
e.name = 'PARSE_ERROR'; throw e;
}
/// Combinators
function choice_(m,k) {
return function (s) {
try {
return m(s);
}
catch (e) {
switch (e.name) {
case 'PARSE_ERROR':
if (e.message[1].length != s.length) throw e;
else return k(s);
break;
default:
throw e;
}
}
}
}
function try_(m) {
return function (s) {
try {
return m(s);
}
catch (e) {
switch (e.name) {
case 'PARSE_ERROR':
e.message[1] = s; // Pretend we haven't consumed input
throw e;
break;
default:
throw e;
}
}
}
}
var letter_ = doM( 
getS
, bindM
, function (input) {
if (input.length == 0)
return throwInt("Unexpected end of input, expected letter",input);
if (input.match(/^[a-z]/i))
return doM( putS(input.slice(1)) ,thenM, returnM(input[0]) );
else
return throwInt("Expected letter ([a-zA-Z])",input);
}
);
function char_(c) {
return doM(
getS
, bindM
, function (input) {
if (input.length == 0)
return throwInt("Unexpected end of input, expected '" + c + "'",input);
if (input[0] == c)
return doM( putS(input.slice(1)) ,thenM, returnM(c) );
else
return throwInt("Expected character '" + c + "'",input);
}
);
}
function string_(str) {
return mapM(char_,str);
}
// Run a parser
function run(parser,string) {
print("Parse output:");
try {
print(parse(parser,string));
}
catch (e) {
switch (e.name) {
case 'PARSE_ERROR':
print("Parse failed: " + e.message[0] + ", input: " + e.message[1]);
break;
default:
throw e;
}
}
}

108 Name: #!/usr/bin/anonymous : 2008-04-27 05:20 ID:Heaven

>>107

// Example parsers
// Parse some parentheses
function parens(s) {
return choice_( doM( char_('(')
, thenM
, parens
, thenM
, char_(')')
, thenM
, parens
)
, returnM(null)
)(s);
}
var testOr = choice_( string_("(a)")
, string_("(b)") );
var testOr1 = doM( char_('(')
, thenM
, choice_( char_('a') , char_('b') )
, thenM
, char_(')')
);
var testOr2 = choice_( try_( string_("(a)") )
, string_("(b)")
);
var testOr3 = choice_( doM( try_( string_("(a") )
, thenM
, char_(')')
, thenM
, returnM("(a)")
)
, string_("(b)")
);
function nesting(s) { 
return choice_(
doM(
char_('(')
, thenM
, nesting
, bindM
, function (n) {
return doM(
char_(')')
, thenM
, nesting
, bindM
, function (m) {
return returnM(Math.max(n+1, m));
}
);
})
, returnM(0)
)(s);
}

109 Name: #!/usr/bin/anonymous : 2008-04-27 05:22 ID:Heaven

>>108
Usage: var nestcount = run(nesting,"((()))");

110 Name: #!/usr/bin/anonymous : 2008-05-01 05:38 ID:ohi6hRRc

//Coded by drwho
// Knight's Tour program where moves are base on on horizontal and vertical
// arrays and are accessed by a random number generator. This does only 64
// moves but can be increased. the 0 on the board is the night
// I didn't know how to get C to print out both characters and integers
// or else I would have made the knight k (%c didn't work and just displays
// weird shit). Question? I idle in #4-ch on synirc so come by
// http://en.wikipedia.org/wiki/Knight's_tour

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

int main(int argc, char *argv[])
{
int j, currentrow, currentcolumn, lastmover, lastmovec, counta, moves, check;
static movenumber;
int horizontal[8]={2,1,-1,-2,-2,-1,1,2};
int vertical[8]={-1,-2,-2,-1,1,2,2,1};
int board[8][8]={

     {2,3,4,4,4,4,3,2},
{3,4,6,6,6,6,4,3},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{3,4,6,6,6,6,4,3},
{2,3,4,4,4,4,3,2}
};

currentrow = 3;
currentcolumn = 3;
board[currentrow][currentcolumn] = 0;
moves = 0;
check = 0;
//time seed
srand ( time(NULL) );
for (counta = 1; counta < 65; counta++){

printf("Knights Round Program\n");
// random move number with time seed
movenumber = rand() % 8;
currentrow += horizontal[movenumber];
currentcolumn += vertical[movenumber];

if(currentrow > 8 || currentrow < 0 || currentcolumn > 8 || currentcolumn < 0){
currentrow = lastmover;
currentcolumn = lastmovec;
}
check = board[currentrow][currentcolumn];
if(check == 0){ board[lastmover][lastmovec]; }
else{board[currentrow][currentcolumn] = 0;}

// The Board
printf("\n");
for(j=0;j<8;j++){printf("%d", board[0][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[1][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[2][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[3][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[4][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[5][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[6][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[7][j]);}
printf("\n");
printf("Row: %d | Column: %d\n", currentrow, currentcolumn);
if(currentrow != lastmover && currentcolumn != lastmovec){moves++;}
lastmover = currentrow;
lastmovec = currentcolumn;
printf("Moves: %d\n", moves);

if(counta == 64){
printf("Knights Round Program\n");
printf("\n");
for(j=0;j<8;j++){printf("%d", board[0][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[1][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[2][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[3][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[4][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[5][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[6][j]);}
printf("\n");
for(j=0;j<8;j++){printf("%d", board[7][j]);}
printf("\n");
printf("Row: %d | Column: %d\n", currentrow, currentcolumn);
printf("Moves: %d\n", moves);
system("PAUSE");
return 0;
}

//Time Delay
sleep(1000);
system("cls");

}

}

111 Name: #!/usr/bin/anonymous : 2008-05-01 07:56 ID:ohi6hRRc

//Coded by drwho
// Update: Thanks to a friend from 2600 I was able to shorten the code for the
// chess board. Now that is real hacking!
// Knight's Tour program where moves are base on on horizontal and vertical
// arrays and are accessed by a random number generator. This does only 64
// moves but can be increased. the 0 on the board is the night
// I didn't know how to get C to print out both characters and integers
// or else I would have made the knight k (%c didn't work and just displays
// weird shit). Question? I idle in #4-ch on synirc so come by
// http://en.wikipedia.org/wiki/Knight's_tour

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

int main(int argc, char *argv[])
{
int j, i, currentrow, currentcolumn, lastmover, lastmovec, counta, moves, check;
static movenumber;
int horizontal[8]={2,1,-1,-2,-2,-1,1,2};
int vertical[8]={-1,-2,-2,-1,1,2,2,1};
int board[8][8]={

     {2,3,4,4,4,4,3,2},
{3,4,6,6,6,6,4,3},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},
{3,4,6,6,6,6,4,3},
{2,3,4,4,4,4,3,2}
};

currentrow = 3;
currentcolumn = 3;
board[currentrow][currentcolumn] = 0;
moves = 0;
check = 0;
//time seed
srand ( time(NULL) );
for (counta = 1; counta < 65; counta++){

printf("Knights Round Program\n");
// random move number with time seed
movenumber = rand() % 8;
currentrow += horizontal[movenumber];
currentcolumn += vertical[movenumber];

if(currentrow > 8 || currentrow < 0 || currentcolumn > 8 || currentcolumn < 0){
currentrow = lastmover;
currentcolumn = lastmovec;
}
check = board[currentrow][currentcolumn];
if(check == 0){ board[lastmover][lastmovec]; }
else{board[currentrow][currentcolumn] = 0;}

// The Board
printf("\n");
for(i=0;i<8;i++) { for(j=0;j<8;j++){printf("%d", board[i][j]);} printf("\n"); }
printf("\n");
printf("Row: %d | Column: %d\n", currentrow, currentcolumn);
if(currentrow != lastmover && currentcolumn != lastmovec){moves++;}
lastmover = currentrow;
lastmovec = currentcolumn;
printf("Moves: %d\n", moves);

if(counta == 64){
printf("Knights Round Program\n");
printf("\n");
for(i=0;i<8;i++) { for(j=0;j<8;j++){printf("%d", board[i][j]);} printf("\n"); }
printf("\n");
printf("Row: %d | Column: %d\n", currentrow, currentcolumn);
printf("Moves: %d\n", moves);
system("PAUSE");
return 0;
}

//Time Delay
sleep(1000);
system("cls");

}

}

112 Name: #!/usr/bin/anonymous : 2008-05-02 16:51 ID:Heaven

113 Name: #!/usr/bin/anonymous : 2014-04-10 20:19 ID:mTcepw93

>>1
Why not just implement an interpreter for lisp or something. That's as useful as you can get because you can implement any other program in that.

114 Name: #!/usr/bin/anonymous : 2014-04-12 08:58 ID:OPaAx9vm

ok I hav it giv me a sec XDD

[code]
mysqld > /dev/usr/in > out.py
out.py < in.tt > ee.ttt
dc -e "5 6 * + +" > ff.rrrr
cat ee.ttt ff.rrrr > src.c
gcc -Wall -O3 src.c -o exe
[/code]

ders my code XDD u need the /devusr/in file tho XDDD

Name: Link:
Leave these fields empty (spam trap):
More options...
Verification: