Commits (8)
pushq %rbp
movq %rsp, %rbp
subq $816, %rsp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $8, %rsp
\ No newline at end of file
import lib.BufferedReader;
class Echo {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader().init();
Str str = reader.readLine();
while (str != null) {
str.println();
str = reader.readLine();
}
}
}
\ No newline at end of file
GET / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
GET /favicon.ico HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
import lib.HttpServer;
class EchoServer {
public static void main(String[] args) throws Exception {
Application app = new Application();
new HttpServer().init(app).run();
}
}
class Application {
public HttpServer server;
public void init(HttpServer server) {
this.server = server;
}
public void processRequest(Request request) {
Str str1 = new Str().init11(89, 111, 117, 32, 109, 97, 100, 101, 32, 97, 32); /* You made a */
Str str2 = new Str().init12(32, 114, 101, 113, 117, 101, 115, 116, 32, 116, 111, 32); /* request to */
Str content = str1.concat(request.getMethod()).concat(str2).concat(request.getUrl());
Response response = new Response().init(200, content);
server.sendResponse(response);
}
}
\ No newline at end of file
GET / HTTP/1.0
GET /13 HTTP/1.0
GET /21 HTTP/1.0
\ No newline at end of file
import lib.HttpServer;
import lib.Str;
import lib.HtmlBuilder;
class FibServer {
public static void main(String[] args) throws Exception {
Application app = new Application();
new HttpServer().init(app).run();
}
}
class Application {
public HttpServer server;
public int count;
public void init(HttpServer server) {
this.server = server;
}
public void processRequest(Request request) {
HtmlBuilder builder = new HtmlBuilder();
HtmlElement html = builder.html();
HtmlElement header = html.head();
HtmlElement body = html.body();
Str fibonacci = new Str().init9(70, 105, 98, 111, 110, 97, 99, 99, 105); /* Fibonacci */
header.title().text(fibonacci);
if (request.getUrl().equals(new Str().init1(47))) { /* / */
body.h(1).text(fibonacci);
body.p().text(new Str().init20(87, 104, 105, 99, 104, 32, 70, 105, 98, 111, 110, 97, 99, 99, 105, 32, 110, 117, 109, 98)
.concat(new Str().init20(101, 114, 32, 100, 111, 32, 121, 111, 117, 32, 119, 97, 110, 116, 32, 116, 111, 32, 107, 110)
.concat(new Str().init3(111, 119, 63)))); /* Which Fibonacci number do you want to know? */
int nr = 0;
while (nr <= 40) {
Str intStr = new Str().fromInt(nr);
HtmlElement a = body.a(new Str().init1(47).concat(intStr));
a.text(intStr);
body.br();
nr = nr + 1;
}
} else {
/* parse the part behind the url as a number */
Str numberStr = request.getUrl().substring(1, request.getUrl().length);
int number = numberStr.toInt();
Str title = new Str().init20(84, 104, 101, 32, 70, 105, 98, 111, 110, 97, 99, 99, 105, 32, 110, 117, 109, 98, 101, 114)
.concat(new Str().init4(32, 111, 102, 32)); /* The fibonacci number of */
body.h(1).text(title.concat(numberStr).concat(new Str().init4(32, 105, 115, 58))); /* is: */
body.text(new Str().fromInt(new Fib().fib(number)));
body.br();body.br();body.br();
HtmlElement a = body.a(new Str().init1(47));
a.text(new Str().init20(67, 97, 108, 99, 117, 108, 97, 116, 101, 32, 97, 110, 111, 116, 104, 101, 114, 32, 111, 110).concat(new Str().init1(101))); /* Calculate another one */
}
Str content = html.buildToString();
Response response = new Response().init(200, content);
server.sendResponse(response);
}
}
class Fib {
public int fib(int n) {
if(n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
}
File mode changed from 100755 to 100644
import lib.Str;
class HelloWord {
public static void main(String[] args) {
new Str().init12(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33).println(); /* Hello World! */
}
}
\ No newline at end of file
import lib.AnsiTerminal;
import lib.TerminalInput;
import lib.BooleanMatrix;
import lib.LCG;
import lib.Math;
import lib.Str;
/**
* A simple snake game.
*
* Use the wrapper to get the full experience:
* https://git.scc.kit.edu/ufebl/mjtest-scripts/blob/master/mj_terminal.sh
*/
class Game {
public AnsiTerminal terminal;
public TerminalInput input;
public int x;
public int y;
public BooleanMatrix snakeField;
public SnakeElement tail;
public BooleanMatrix appleField;
public LCG random;
public int score;
public int direction;
public int nextDirection;
public int gameOver; /* game over counter */
public Game init(AnsiTerminal terminal) {
this.terminal = terminal;
return this;
}
public void setInput(TerminalInput input) {
this.input = input;
}
public void setup() {
x = 1;
y = 1;
direction = 1;
nextDirection = 1;
terminal.set24BitBgColor(0, 0, 10);
terminal.clearScreen();
terminal.conceal();
random = new LCG().init(input.getColumns() * input.getLines());
snakeField = new BooleanMatrix().init(input.getColumns(), input.getLines());
appleField = new BooleanMatrix().init(input.getColumns(), input.getLines());
tail = new SnakeElement().init(x, y);
printSnakeHead();
right();
printSnakeHead();
right();
printSnakeHead();
int i = 0;
int appleCount = new Math().min(input.getColumns(), input.getLines()) / 2;
while (i < appleCount) {
spawnApple();
i = i + 1;
}
printScore();
terminal.flush();
}
public void printSnakeHead() {
snakeField.set(x - 1, y - 1, true);
terminal.setCursor(x, y);
terminal.set24BitBgColor(0, 230, 0);
System.out.write(78);
terminal.setCursor(x, y);
tail.append(new SnakeElement().init(x, y));
}
public void eraseSnakeTail() {
snakeField.set(tail.getX() - 1, tail.getY() - 1, false);
terminal.setCursor(tail.getX(), tail.getY());
terminal.set24BitBgColor(0, 0, 10);
System.out.write(78);
tail = tail.getNext(); /* eleminate one element */
}
public void spawnApple() {
int x = random.next() % input.getColumns() + 1;
int y = random.next() % input.getLines() + 1;
if (!snakeField.get(x - 1, y - 1)) {
/* place free to spawn here */
appleField.set(x - 1, y - 1, true);
terminal.bold();
terminal.notConceal();
terminal.set24BitFgColor(230, 70, 0);
terminal.set24BitBgColor(0, 0, 10);
terminal.setCursor(x, y);
System.out.write(111); /* o */
terminal.setCursor(x, y); /* set cursor back to snake position */
terminal.conceal();
terminal.normalWeight();
terminal.fgDefault();
}
}
public void printScore() {
terminal.setCursor(2, 2);
terminal.fgDefault();
terminal.set24BitBgColor(0, 0, 10);
terminal.notConceal();
new Str().fromInt(score).print();
terminal.conceal();
}
public void keyPressed(int key) {
if (key == input.LEFT) {
if (direction != 1) {
nextDirection = 0;
}
} else if (key == input.RIGHT) {
if (direction != 0) {
nextDirection = 1;
}
} else if (key == input.UP) {
if (direction != 3) {
nextDirection = 2;
}
} else if (key == input.DOWN) {
if (direction != 2) {
nextDirection = 3;
}
} else if (key == input.ESCAPE) {
quit();
return;
} else if (key == input.TICK) {
if (gameOver > 0) {
gameOver = gameOver + 1;
if (gameOver > 20) {
quit();
}
return;
}
direction = nextDirection;
if (direction == 0) {
left();
} else if (direction == 1) {
right();
} else if (direction == 2) {
up();
} else {
down();
}
if (appleField.get(x - 1, y - 1)) {
appleField.set(x - 1, y - 1, false);
/* don't erase the tail -> the snake's size increases by one */
score = score + 1;
printScore();
spawnApple();
} else {
eraseSnakeTail();
}
if (random.next() % 100 == 0) {
spawnApple();
}
if (snakeField.get(x - 1, y - 1)) {
/* GAME OVER */
gameOver = 1;
terminal.set24BitBgColor(255, 0, 0);
terminal.setCursor(x, y);
System.out.write(32);
terminal.setCursorToOrigin();
terminal.flush();
return;
}
printSnakeHead();
terminal.flush();
}
}
public void left() {
x = x - 1;
if (x <= 0) {
x = input.getColumns();
}
}
public void right() {
x = x + 1;
if (x > input.getColumns()) {
x = 1;
}
}
public void up() {
y = y - 1;
if (y <= 0) {
y = input.getLines();
}
}
public void down() {
y = y + 1;
if (y > input.getLines()) {
y = 1;
}
}
public void quit() {
terminal.resetColor();
terminal.clearScreen();
terminal.setCursorToOrigin();
terminal.flush();
input.quit();
}
public static void main(String[] args) throws Exception {
AnsiTerminal terminal = new AnsiTerminal();
Game game = new Game().init(terminal);
InputReciever inputReciever = new InputReciever().initGame(game);
new TerminalInput().init(terminal, inputReciever).handleInput();
}
}
class SnakeElement {
public int x;
public int y;
public SnakeElement next;
public SnakeElement init(int x, int y) {
this.x = x;
this.y = y;
return this;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public SnakeElement getNext() {
return next;
}
public void append(SnakeElement snakeElement) {
if (next == null) {
next = snakeElement;
} else {
next.append(snakeElement);
}
}
}
class InputReciever {
public Game game;
public InputReciever initGame(Game game) {
this.game = game;
return this;
}
public void init(TerminalInput terminalInput) {
game.setInput(terminalInput);
game.setup();
}
public void keyPressed(int key) {
game.keyPressed(key);
}
}
This diff is collapsed.
This diff is collapsed.
import lib.AsciiPgmImage;
class Test {
public static void main(String[] args) throws Exception {
AsciiPgmImage image = new AsciiPgmImage().readFromStdin();
new GaussianBlur().init().applyToImage(image).printToStdout();
}
}
class GaussianBlur {
public int kernelSize;
public int[][] kernel;
public int kernelDivisor;
public GaussianBlur initSmall() {
kernelSize = 5;
kernelDivisor = 256;
kernel = new int[kernelSize][];
kernel[0] = line5(1, 4, 6, 4, 1);
kernel[1] = line5(4, 16, 24, 16, 4);
kernel[2] = line5(6, 24, 36, 24, 6);
kernel[3] = line5(4, 16, 24, 16, 4);
kernel[4] = line5(1, 4, 6, 4, 1);
return this;
}
public int[] line5(int a, int b, int c, int d, int e) {
int[] result = new int[5];
result[0] = a;
result[1] = b;
result[2] = c;
result[3] = d;
result[4] = e;
return result;
}
public GaussianBlur init() {
kernelSize = 7;
kernelDivisor = 4096;
kernel = new int[kernelSize][];
kernel[0] = line7( 1, 6, 15, 20, 15, 6, 1);
kernel[1] = line7( 6, 36, 90, 120, 90, 36, 6);
kernel[2] = line7(15, 90, 225, 300, 225, 90, 15);
kernel[3] = line7(20, 120, 300, 400, 300, 120, 20);
kernel[4] = line7(15, 90, 225, 300, 225, 90, 15);
kernel[5] = line7( 6, 36, 90, 120, 90, 36, 6);
kernel[6] = line7( 1, 6, 15, 20, 15, 6, 1);
return this;
}
public int[] line7(int a, int b, int c, int d, int e, int f, int g) {
int[] result = new int[7];
result[0] = a;
result[1] = b;
result[2] = c;
result[3] = d;
result[4] = e;
result[5] = f;
result[6] = g;
return result;
}
public AsciiPgmImage applyToImage(AsciiPgmImage image) {
AsciiPgmImage newImage = new AsciiPgmImage();
newImage.width = image.width;
newImage.height = image.height;
newImage.maxValue = image.maxValue;
newImage.data = new int[image.width * image.height];
int y = 0;
while (y < image.height) {
int x = 0;
while (x < image.width) {
newImage.data[x + y * image.width] = calcValueAtPixel(image, x, y);
x = x + 1;
}
y = y + 1;
}
return newImage;
}
public int calcValueAtPixel(AsciiPgmImage image, int x, int y) {
int val = 0;
int cY = 0;
while (cY < kernelSize) {
int cX = 0;
while (cX < kernelSize) {
val = val + kernel[cY][cX] * get(image, x + cX - kernelSize/2, y + cY - kernelSize/2);
cX = cX + 1;
}
cY = cY + 1;
}
return val / kernelDivisor;
}
public int get(AsciiPgmImage image, int x, int y) {
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (x >= image.width) {
x = image.width - 1;
}
if (y >= image.height) {
y = image.height - 1;
}
return image.get(x, y);
}
}
package lib;
/**
* This helps you output ANSI codes to make your terminal output fancy.
* see https://en.wikipedia.org/wiki/ANSI_excape_code#Escape_sequences
*/
public class AnsiTerminal {
public void flush() {
System.out.flush();
}
public void esc() {
System.out.write(27); /* ESC */
}
public void csi() {
esc();
System.out.write(91); /* [ */
}
public void reset() {
esc();
System.out.write(99); /* c */
}
public void cursorUp() {
cursorUpN(1);
}
public void cursorUpN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(65); /* A */
}
public void cursorDown() {
cursorDownN(1);
}
public void cursorDownN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(66); /* B */
}
public void cursorForward() {
cursorForwardN(1);
}
public void cursorForwardN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(67); /* C */
}
public void cursorBack() {
cursorBackN(1);
}
public void cursorBackN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(68); /* D */
}
public void cursorNextLine() {
cursorNextLineN(1);
}
public void cursorNextLineN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(69); /* E */
}
public void cursorPreviousLine() {
cursorPreviousLineN(1);
}
public void cursorPreviousLineN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(70); /* F */
}
public void cursorSetColumn(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(71); /* G */
}
public void setCursorToOrigin() {
setCursor(1, 1);
}
/**
* Set the position of the cursor. Top left is 1,1.
*/
public void setCursor(int x, int y) {
csi();
if (y != 1) {
new Str().fromInt(y).print();
}
if (x != 1 || y != 1) {
System.out.write(59); /* ; */
}
if (x != 1) {
new Str().fromInt(x).print();
}
System.out.write(72); /* H */
}
public void eraseInDisplay(int n) {
if (n < 4) {
csi();
System.out.write(48 + n);
System.out.write(74); /* J */
}
}
public void clearScreen() {
eraseInDisplay(2);
}
public void eraseInLine(int n) {
if (n < 3) {
csi();
System.out.write(48 + n);
System.out.write(75); /* K */
}
}
public void scrollUp(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(83); /* S */
}
public void scrollDown(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(84); /* T */
}
public void selectGraphicRendition(int n) {
csi();
new Str().fromInt(n).print();
System.out.write(109); /* m */
}
public void saveCursorPosition() {
csi();
System.out.write(115); /* s */
}
public void restoreCursorPosition() {
csi();
System.out.write(116); /* t */
}
public void showCursor() {
csi();
System.out.write(63); /* ? */
System.out.write(50); /* 2 */
System.out.write(53); /* 5 */
System.out.write(104); /* h */
}
public void hideCursor() {
csi();
System.out.write(63); /* ? */
System.out.write(50); /* 2 */
System.out.write(53); /* 5 */
System.out.write(108); /* l */
}
/* SGR */
public void resetColor() {
selectGraphicRendition(0);
}
public void bold() {
selectGraphicRendition(1);
}
public void faint() {
selectGraphicRendition(2);
}
public void italic() {
selectGraphicRendition(3);
}
public void underline() {
selectGraphicRendition(4);
}
public void slowBlink() {
selectGraphicRendition(5);
}
public void rapidBlink() {
selectGraphicRendition(6);
}
public void swapForegroundAndBackgroundColors() {
selectGraphicRendition(7);
}
public void conceal() {
selectGraphicRendition(8);
}
public void crossedOut() {
selectGraphicRendition(9);
}
/* n in 0 - 9 */
public void selectFont(int n) {
selectGraphicRendition(n + 10);
}
public void fraktur() {
selectGraphicRendition(20);
}
public void doubleUnderline() {
selectGraphicRendition(21);
}
public void normalWeight() {
selectGraphicRendition(22);
}
public void notItalic() {
selectGraphicRendition(23);
}
public void notUnderline() {
selectGraphicRendition(24);
}
public void notBlink() {
selectGraphicRendition(25);
}
public void notInversed() {
selectGraphicRendition(27);
}
public void notConceal() {
selectGraphicRendition(28);
}
public void notCrossedOut() {
selectGraphicRendition(29);
}
public void fgBlack() {
selectGraphicRendition(30);
}
public void fgRed() {
selectGraphicRendition(31);
}
public void fgGreen() {
selectGraphicRendition(32);
}
public void fgYellow() {
selectGraphicRendition(33);
}
public void fgBlue() {
selectGraphicRendition(34);
}
public void fgMagenta() {
selectGraphicRendition(35);
}
public void fgCyan() {
selectGraphicRendition(36);
}
public void fgWhite() {
selectGraphicRendition(37);
}
public void fgDefault() {
selectGraphicRendition(39);
}
public void bgBlack() {
selectGraphicRendition(40);
}
public void bgRed() {
selectGraphicRendition(41);
}
public void bgGreen() {
selectGraphicRendition(42);
}
public void bgYellow() {
selectGraphicRendition(43);
}
public void bgBlue() {
selectGraphicRendition(44);
}
public void bgMagenta() {
selectGraphicRendition(45);
}
public void bgCyan() {
selectGraphicRendition(46);
}
public void bgWhite() {
selectGraphicRendition(47);
}
public void bgDefault() {
selectGraphicRendition(49);
}
public void framed() {
selectGraphicRendition(51);
}
public void encircled() {
selectGraphicRendition(52);
}
public void overlined() {
selectGraphicRendition(53);
}
public void notFramedOrEncircled() {
selectGraphicRendition(54);
}
public void notOverlined() {
selectGraphicRendition(55);
}
/* https://en.wikipedia.org/wiki/ANSI_excape_code#8-bit */
public void set8BitFgColor(int n) {
csi();
System.out.write(51); /* 3 */
System.out.write(56); /* 8 */
System.out.write(59); /* ; */
System.out.write(53); /* 5 */
System.out.write(59); /* ; */
new Str().fromInt(n).print();
System.out.write(109); /* m */
}
/* https://en.wikipedia.org/wiki/ANSI_excape_code#8-bit */
public void set8BitBgColor(int n) {
csi();
System.out.write(52); /* 4 */
System.out.write(56); /* 8 */
System.out.write(59); /* ; */
System.out.write(53); /* 5 */
System.out.write(59); /* ; */
new Str().fromInt(n).print();
System.out.write(109); /* m */
}
public void set24BitFgColor(int r, int g, int b) {
csi();
System.out.write(51); /* 3 */
System.out.write(56); /* 8 */
System.out.write(59); /* ; */
System.out.write(50); /* 2 */
System.out.write(59); /* ; */
new Str().fromInt(r).print();
System.out.write(59); /* ; */
new Str().fromInt(g).print();
System.out.write(59); /* ; */
new Str().fromInt(b).print();
System.out.write(109); /* m */
}
public void set24BitBgColor(int r, int g, int b) {
csi();
System.out.write(52); /* 4 */
System.out.write(56); /* 8 */
System.out.write(59); /* ; */
System.out.write(50); /* 2 */
System.out.write(59); /* ; */
new Str().fromInt(r).print();
System.out.write(59); /* ; */
new Str().fromInt(g).print();
System.out.write(59); /* ; */
new Str().fromInt(b).print();
System.out.write(109); /* m */
}
}
package lib;
import lib.BooleanUtils;
public class ArrayUtils {
public ArrayUtils printIntArray(int[] arr, int size) {
int i = 0;
while (i < size) {
System.out.println(arr[i]);
i = i + 1;
}
return this;
}
public ArrayUtils printBooleanArray(boolean[] arr, int size) {
int i = 0;
while (i < size) {
new BooleanUtils().println(arr[i]);
i = i + 1;
}
return this;
}
public int[] copyIntArray(int[] src, int start, int length) {
int[] ret = new int[length];
int i = start;
while (i < start + length) {
ret[i - start] = src[i];
i = i + 1;
}
return ret;
}
public int toInt(int[] chars, int size) {
int val = 0;
size = size - 1;
while (size >= 0) {
val = val * 10 + chars[size] - 48;
size = size - 1;
}
return val;
}
public int[] qsort(int[] a, int size) {
_qsort(a, 0, size - 1);
return a;
}
/** Adapted from http://stackoverflow.com/a/29610583 */
public void _qsort(int[] a, int left, int right) {
if (right > left) {
int i = left;
int j = right;
int tmp;
int v = a[right];
boolean breakLoop = false;
while (!breakLoop) {
while (a[i] < v)
i = i + 1;
while (a[j] > v)
j = j - 1;
if (i <= j) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i = i + 1;
j = j - 1;
}
if (i > j) {
breakLoop = true;
}
}
if (left < j)
_qsort(a, left, j);
if (i < right)
_qsort(a, i, right);
}
}
public int[] _marr;
public int[] msort(int[] arr, int size) {
this._marr = arr;
_msort(0, size - 1);
return _marr;
}
public void _msort(int low, int high) {
if (low < high) {
int mid = ((low + high) / 2);
_msort(low, mid);
_msort(mid + 1, high);
_msort_merge(low, mid, high);
}
}
/*
* Adapted from http://stackoverflow.com/a/20039399
*/
public void _msort_merge(int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int left = low;
int right = mid + 1;
int index = 0;
while (left <= mid && right <= high) {
if (_marr[left] < this._marr[right]) {
temp[index] = _marr[left];
left = left + 1;
} else {
temp[index] = _marr[right];
right = right + 1;
}
index = index + 1;
}
while (left <= mid || right <= high) {
if (left <= mid) {
temp[index] = _marr[left];
left = left + 1;
index = index + 1;
} else if (right <= high) {
temp[index] = _marr[right];
right = right + 1;
index = index + 1;
}
}
int i = 0;
while (i < high - low + 1) {
_marr[low + i] = temp[i];
i = i + 1;
}
}
}
\ No newline at end of file
package lib;
/** Reads Portable Graymap (.pgm) images exported with GIMP in ASCII format */
public class AsciiPgmImage {
public int[] data;
public int width;
public int height;
public int maxValue;
public AsciiPgmImage readFromStdin() throws Exception {
int magicNumber = readIntFromStdin();
if (magicNumber != 2) {
/* this is not a pgm in ASCII format */
return this;
}
width = readIntFromStdin();
height = readIntFromStdin();
maxValue = readIntFromStdin();
data = new int[width * height];
int i = 0;
while (i < width * height) {
data[i] = readIntFromStdin();
i = i + 1;
}
return this;
}
public int readIntFromStdin() throws Exception {
int result = 0;
int c = System.in.read();
if (c == 35) { /* # at the beginning of the line, this is a comment */
while (c != 10) { /* ignore the whole line */
c = System.in.read();
}
c = System.in.read();
}
while (c != 10 && c != 32) { /* until newline or space */
if (c > 57 || c < 48) {
/* ignore non-digits */
} else {
int digit = c - 48; /* convert to digit */
result = result * 10 + digit;
}
c = System.in.read();
}
return result;
}
public void printToStdout() {
System.out.write(80); /* P */
System.out.println(2); /* correct magic number */
System.out.println(width);
System.out.println(height);
System.out.println(maxValue);
int i = 0;
while (i < width * height) {
System.out.println(data[i]);
i = i + 1;
}
}
public int get(int x, int y) {
return data[x + y * width];
}
public void set(int x, int y, int value) {
data[x + y * width] = value;
}
}
package lib;
public class BooleanMatrix {
public boolean[][] data;
public BooleanMatrix init(int dim1, int dim2) {
data = new boolean[dim1][];
int i = 0;
while (i < dim1) {
data[i] = new boolean[dim2];
i = i + 1;
}
return this;
}
public boolean get(int d1, int d2) {
return data[d1][d2];
}
public void set(int d1, int d2, boolean value) {
data[d1][d2] = value;
}
}
\ No newline at end of file
package lib;
import lib.Str;
/**
* This class helps to read the stdin line by line and returns Str strings. You
* need to remember to call new BufferedReader().init() to initialize it.
*/
public class BufferedReader {
public int BUFFER_SIZE;
public BufferedReader init() {
BUFFER_SIZE = 16;
return this;
}
/* Use this init method in case you already know how big your input is */
public BufferedReader initBufferSize(int bufferSize) {
this.BUFFER_SIZE = bufferSize;
return this;
}
public Str readLine() throws Exception {
int c = System.in.read();
if (c == -1) { /* There is nothing to read */
return null;
}
Str result = null;
int[] buffer = new int[BUFFER_SIZE];
int index = 0;
while (c != 10 && c != 0 && c != -1) {
if (index >= BUFFER_SIZE) {
Str newStr = new Str().init(buffer, BUFFER_SIZE);
if (result == null) {
result = newStr;
} else {
result = result.concat(newStr);
}
buffer = new int[BUFFER_SIZE];
index = 0;
}
buffer[index] = c;
index = index + 1;
c = System.in.read();
}
Str newStr = new Str().init(buffer, index);
if (result == null) {
return newStr;
} else {
return result.concat(newStr);
}
}
}
\ No newline at end of file
package lib;
import lib.Str;
/**
* This class helps with quickly building html layouts without writing much
* code.
*/
public class HtmlBuilder {
public HtmlElement root;
public Str buildToString() {
if (root != null) {
return root.buildToString();
} else {
return new Str().init0();
}
}
public HtmlElement html() {
Str start = new Str().init6(60, 104, 116, 109, 108, 62);
/* <html> */;
Str end = new Str().init7(60, 47, 104, 116, 109, 108, 62); /* </html> */
root = new HtmlElement().init(start, end);
return root;
}
}
class HtmlElement {
public Str start;
public Str end;
public HtmlElement next; /* next element in this list */
public HtmlElement children; /* children in the next inner level */
public HtmlElement init(Str start, Str end) {
this.start = start;
this.end = end;
return this;
}
public Str buildToString() {
Str result = start;
/* children are inside */
if (children != null) {
result = result.concat(children.buildToString());
}
if (end != null) {
result = result.concat(end);
}
if (next != null) {
result = result.concat(next.buildToString());
}
return result;
}
public void appendToList(HtmlElement newElement) {
if (next == null) {
next = newElement;
} else {
next.appendToList(newElement);
}
}
public void addChild(HtmlElement newElement) {
if (children == null) {
children = newElement;
} else {
children.appendToList(newElement);
}
}
/* helper functions to quickly create elements */
public HtmlElement head() {
Str start = new Str().init6(60, 104, 101, 97, 100, 62); /* <head> */
Str end = new Str().init7(60, 47, 104, 101, 97, 100, 62); /* </head> */
HtmlElement element = new HtmlElement().init(start, end);
addChild(element);
return element;
}
public HtmlElement body() {
Str start = new Str().init6(60, 98, 111, 100, 121, 62); /* <body> */
Str end = new Str().init7(60, 47, 98, 111, 100, 121, 62); /* </body> */
HtmlElement element = new HtmlElement().init(start, end);
addChild(element);
return element;
}
public HtmlElement title() {
Str start = new Str().init7(60, 116, 105, 116, 108, 101, 62); /* <title> */
Str end = new Str().init8(60, 47, 116, 105, 116, 108, 101, 62); /* </title> */
HtmlElement element = new HtmlElement().init(start, end);
addChild(element);
return element;
}
public HtmlElement text(Str string) {
HtmlElement element = new HtmlElement().init(string, null);
addChild(element);
return element;
}
public HtmlElement h(int level) {
Str start = new Str().init4(60, 104, 48 + level, 62); /* <h[X]> */
Str end = new Str().init5(60, 47, 104, 48 + level, 62); /* </h[X]> */