...
 
Commits (27)
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
/*
* Reads a brainfuck program (of no more than 1024 instructions) from stdin and executes it.
*
* Examples:
* Read two chars, add their ASCII values, print the result: ,>,<[->+<]>.
* Hello World: ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
*/
class Brainfuck {
public int RIGHT; /* > */
public int LEFT; /* < */
public int INCREMENT; /* + */
public int DECREMENT; /* - */
public int WRITE; /* . */
public int READ; /* , */
public int LOOP_START; /* [ */
public int LOOP_END; /* ] */
public int NEWLINE; /* \n */
public int[] data;
public int dataSize;
public int dataPtr;
public int[] instructions;
public int instructionsSize;
public int instructionPtr;
public void init() {
RIGHT = 62;
LEFT = 60;
INCREMENT = 43;
DECREMENT = 45;
WRITE = 46;
READ = 44;
LOOP_START = 91;
LOOP_END = 93;
NEWLINE = 10;
dataSize = 30000;
data = new int[dataSize];
dataPtr = 0;
instructionsSize = 1024;
instructions = new int[instructionsSize];
}
public int read() throws Exception {
int result = NEWLINE;
while (result == NEWLINE) {
result = System.in.read();
}
return result;
}
public void fuck() throws Exception {
instructionPtr = 0;
while (instructionPtr < instructionsSize && instructions[instructionPtr] != 0) {
int instruction = instructions[instructionPtr];
if (instruction == RIGHT) {
dataPtr = (dataPtr + 1) % dataSize;
} else if(instruction == LEFT) {
dataPtr = (dataPtr - 1) % dataSize;
} else if (instruction == INCREMENT) {
data[dataPtr] = data[dataPtr] + 1;
} else if (instruction == DECREMENT) {
data[dataPtr] = data[dataPtr] - 1;
} else if (instruction == READ) {
data[dataPtr] = read();
} else if (instruction == WRITE) {
System.out.write(data[dataPtr]);
System.out.flush();
} else if (instruction == LOOP_START && data[dataPtr] == 0) {
int index = instructionPtr + 1;
int count = 0;
while (! (instructions[index] == LOOP_END && count == 0)) {
if (instructions[index] == LOOP_START) {
count = count + 1;
} else if (instructions[index] == LOOP_END) {
count = count - 1;
}
index = index + 1;
}
instructionPtr = index;
} else if (instruction == LOOP_END && data[dataPtr] != 0) {
int index = instructionPtr - 1;
int count = 0;
while (! (instructions[index] == LOOP_START && count == 0)) {
if (instructions[index] == LOOP_END) {
count = count + 1;
} else if (instructions[index] == LOOP_START) {
count = count - 1;
}
index = index - 1;
}
instructionPtr = index;
}
instructionPtr = instructionPtr + 1;
}
}
public static void main(String[] args) throws Exception {
Brainfuck fuck = new Brainfuck();
fuck.init();
int index = 0;
int instruction = System.in.read();
while (index < fuck.instructionsSize && instruction != fuck.NEWLINE) {
fuck.instructions[index] = instruction;
instruction = System.in.read();
index = index + 1;
}
fuck.fuck();
}
}
/*
* Expected output:
* 1
* 1
* 1
* 1
*
* 0
* 0
* 0
* 0
* 0
* 0
*/
class Comp {
public void printBoolean(boolean b) {
if (b) {
System.out.println(1);
} else {
System.out.println(0);
}
}
public static void main(String[] args) {
Comp a = new Comp();
Comp b = a;
Comp c = new Comp();
Comp d = null;
a.printBoolean(a == b);
a.printBoolean(b == a);
a.printBoolean(d == null);
a.printBoolean(null == null);
System.out.write(10); /* new line */
a.printBoolean(a == c);
a.printBoolean(b == c);
a.printBoolean(a == null);
a.printBoolean(b == null);
a.printBoolean(null == a);
a.printBoolean(null == b);
}
}
\ No newline at end of file
/*
* Expected output: All primes smaller than 100.
*/
class Eratosthenes {
public int[] result;
public int count;
public void sieve(int max) {
boolean[] sieve = new boolean[max];
count = 0;
/* Initialize array. */
{
sieve[0] = false;
sieve[1] = false;
int i = 2;
while (i < max) {
sieve[i] = true;
i = i + 1;
}
}
/* Sieve of Eratosthenes algorithm. */
{
int prime = 2;
while (prime < max) {
if (sieve[prime]) {
count = count + 1;
/* Deliberately unoptimized (strength reduction is possible). */
int mult = prime;
while (prime * mult < max) {
sieve[prime * mult] = false;
mult = mult + 1;
}
}
prime = prime + 1;
}
}
result = new int[count];
{
int i = 0;
int j = 0;
while (i < max) {
if (sieve[i]) {
result[j] = i;
j = j + 1;
}
i = i + 1;
}
}
}
}
class Main {
public static void main(String[] args) throws Exception {
Eratosthenes e = new Eratosthenes();
e.sieve(100);
{
int i = 0;
while (i < e.count) {
System.out.println(e.result[i]);
i = i + 1;
}
}
}
}
class FieldInit {
public int i;
public FieldInit f;
public boolean b;
public static void main(String[] args) {
FieldInit f = new FieldInit();
System.out.println(f.i);
if(!f.b) {
System.out.println(1);
}
if(f.f == null) {
System.out.println(2);
}
}
}
/*
* Expected output:
*
* 2^0
* *1
* .4194304
*
* 2^-1
* *1
* .8360759
*/
class FloatArithmetic {
public int _2p8;
public int _2p11;
public int _2p16;
public int _2p23;
public int _2p24;
public int _int_min;
public int _f1_5;
public int _fNaN;
public int _seemlinglyArbitraryConstant;
public int _exponentBias;
public void init() {
_2p8 = 256;
_2p11 = 2048;
_2p16 = 65536;
_2p23 = 8388608;
_2p24 = 16777216;
_int_min = -2147483648;
_f1_5 = 1069547520;
_fNaN = -1;
_seemlinglyArbitraryConstant = 1597463007;
_exponentBias = 127;
}
public void print(int x) {
int TWO = 50;
int POWER = 94;
int MINUS = 45;
int TIMES = 42;
int DOT = 46;
int NEWLINE = 10;
int mantissa = mantissa(x) + _2p23;
int exponent = exponent(x) - _exponentBias - 23;
int sign = sign(x);
if (sign == 1) {
System.out.write(MINUS);
}
int oldExponent = exponent;
int mantissaPreDec = mantissa;
while (mantissaPreDec != 1) {
mantissaPreDec = mantissaPreDec / 2;
exponent = exponent + 1;
}
int mantissaPreDecShifted = mantissaPreDec;
while (oldExponent < exponent) {
mantissaPreDecShifted = mantissaPreDecShifted * 2;
oldExponent = oldExponent + 1;
}
System.out.write(TWO);
System.out.write(POWER);
System.out.println(exponent);
System.out.write(TIMES);
System.out.println(mantissaPreDec);
System.out.write(DOT);
System.out.println(mantissa % mantissaPreDecShifted);
System.out.write(NEWLINE);
System.out.flush();
}
public int buildFloat(int sign, int exponent, int mantissa) {
int result = exponent * _2p23 + mantissa;
if (sign == 1) {
result = negate(result);
}
return result;
}
public int negate(int x) {
return _int_min + x;
}
public int sign(int x) {
if (x >= 0) {
return 0;
} else {
return 1;
}
}
public int exponent(int x) {
if (x < 0) {
x = negate(x);
}
return x / _2p23 % _2p8;
}
public int mantissa(int x) {
if (x < 0) {
x = negate(x);
}
return x % _2p23;
}
public int div2(int x) {
int mantissa = mantissa(x);
int exponent = exponent(x);
if (exponent == 0) {
return 0;
} else {
return buildFloat(sign(x), exponent(x) - 1, mantissa(x));
}
}
public int add(int x, int y) {
int xm = mantissa(x);
int xe = exponent(x);
int xs = sign(x);
int ym = mantissa(y);
int ye = exponent(y);
int ys = sign(y);
int exponentDiff;
int exponent;
int mantissa;
int sign;
int lm;
int sm;
if (xe > ye) {
exponentDiff = xe - ye;
exponent = xe;
sign = xs;
lm = xm;
sm = ym;
} else {
exponentDiff = ye - xe;
exponent = ye;
lm = ym;
sm = xm;
if (xe < ye || xm < ym) {
sign = ys;
} else {
sign = xs;
}
}
lm = lm + _2p23;
sm = sm + _2p23;
while (exponentDiff > 0) {
sm = sm / 2;
exponentDiff = exponentDiff - 1;
}
if (xs == ys) {
mantissa = lm + sm;
} else {
mantissa = lm - sm;
}
if (mantissa / _2p24 == 1) {
mantissa = mantissa / 2;
mantissa = mantissa % _2p23;
exponent = exponent + 1;
} else {
mantissa = mantissa % _2p23;
}
return buildFloat(sign, exponent, mantissa);
}
public int square(int x) {
return mult(x, x);
}
public int mult(int x, int y) {
int xm = mantissa(x);
int xe = exponent(x);
int xs = sign(x);
int ym = mantissa(y);
int ye = exponent(y);
int ys = sign(y);
xm = xm + _2p23;
ym = ym + _2p23;
int xm_lo = xm % _2p11;
int ym_lo = ym % _2p11;
int xm_hi = xm / _2p11;
int ym_hi = ym / _2p11;
int mantissa_lo = xm * ym;
int mantissa_hi = xm_hi * ym_hi
+ xm_hi * ym_lo / _2p11
+ xm_lo * ym_hi / _2p11
+ (xm_lo * ym_hi + xm_lo * ym_hi + mantissa_lo / _2p11) / _2p11;
int exponent = xe + ye - _exponentBias;
int mantissa = mantissa_hi / 2;
if (mantissa / _2p24 == 1) {
mantissa = mantissa / 2;
mantissa = mantissa % _2p23;
exponent = exponent + 1;
} else {
mantissa = mantissa % _2p23;
}
return buildFloat((xs + ys) % 2, exponent, mantissa);
}
public int invSqrt(int num) {
if (num <= 0) {
return _fNaN;
}
int x2 = div2(num);
int y = _seemlinglyArbitraryConstant - num / 2;
y = mult(y, add(_f1_5, negate(mult(x2, square(y)))));
return y;
}
public static void main(String[] args) {
FloatArithmetic fa = new FloatArithmetic();
fa.init();
fa.print(fa.mult(1065353216, 1069547520)); /* 1.0 * 1.5 */
fa.print(fa.invSqrt(1065353216)); /* 1 / sqrt(1.0) */
}
}
/* Expected output:
*
* 0
*
* 0
* 1
*
* 0
* 1
* 2
*
* 0
* 1
* 2
* 3
*
* 0
* 1
* 2
* 3
* 4
*
*/
class Test {
public static void main(String[] args) {
int[][] arr = new int[5][];
{
int i = 0;
while (i < 5) {
arr[i] = new int[i + 1];
i = i + 1;
}
}
{
int i = 0;
while (i < 5) {
int j = 0;
while (j < i + 1) {
arr[i][j] = j;
j = j + 1;
}
i = i + 1;
}
}
{
int i = 0;
while (i < 5) {
int j = 0;
while (j < i + 1) {
System.out.println(arr[i][j]);
j = j + 1;
}
i = i + 1;
System.out.write(10); /* newline */
}
}
}
}
class A {
public int i;
public static void main(String[] args) {
A a = null;
int i = a.i;
}
}
\ No newline at end of file
class B {
public static void main(String[] args) {
int[] i = new int[4];
int x = 0;
while(x < 4) {
i[x] = (x = x + 1);
}
x = -1;
while(x < 4) {
System.out.println(i[(x = x + 1)]);
}
}
}
1
2
3
4
\ No newline at end of file
20
625
67
120
316
-191
-377
-103
353
384
240
-759
-566
-111
-623
874
-712
370
-372
-240
-288
1
-2147483648
2147483647
class Summator {
public int sum(int[] array, int len) {
int sum = 0;
int i = 0;
while(i < len) {
sum = sum + array[i];
i = i + 1;
}
return sum;
}
public int readInt() throws Exception {
int nextChar;
int sign = 1;
int number = 0;
int asciiZero = 48;
int asciiMinus = 45;
nextChar = System.in.read();
/* Check for sign */
if (nextChar == asciiMinus) {
sign = sign * (-1);
/* Advance */
nextChar = System.in.read();
}
/* CR/LF Characters */
while (nextChar != 10 && nextChar != 13) {
if (nextChar >= asciiZero && nextChar <= (asciiZero + 9)) {
/* Found a digit */
number = number * 10 + nextChar - asciiZero;
}
nextChar = System.in.read();
}
return sign * number;
}
public static void main(String[] args) throws Exception {
Summator acc = new Summator();
int arrayLen = acc.readInt();
int[] array = new int[arrayLen];
int i = 0;
while(i < arrayLen) {
array[i] = acc.readInt();
i = i + 1;
}
int arraySum = acc.sum(array, arrayLen);
System.out.println(arraySum);
}
}
class A {
public static void main(String[] args) {
System.out.println(2 - 1); /* 1 */
System.out.println(4 / 2); /* 2 */
System.out.println(1 + 2); /* 3 */
System.out.println(2 * 2); /* 4 */
System.out.println(11 % 6); /* 5 */
if (false || true) { System.out.println(6); }
if (true || false) { System.out.println(7); }
if (true && false) { } else { System.out.println(8); }
if (true && true) { System.out.println(9); }
if (1 == 1) { System.out.println(10); }
if (1 == 2) { } else { System.out.println(11); }
if (1 != 2) { System.out.println(12); }
if (1 != 1) { } else { System.out.println(13); }
if (1 < 2) { System.out.println(14); }
if (1 < 1) { } else { System.out.println(15); }
if (2 > 1) { System.out.println(16); }
if (1 > 1) { } else { System.out.println(17); }
if (1 <= 1) { System.out.println(18); }
if (2 <= 1) { } else { System.out.println(19); }
if (1 >= 1) { System.out.println(20); }
if (1 >= 2) { } else { System.out.println(21); }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Bools {
public boolean b;
public static void main(String[] args) {
Bools b = new Bools();
if(!b.b) {
System.out.println(1);
}
b.b = false || true;
if(!b.b) {
System.out.println(2);
}
b.b = false || false;
if(!b.b) {
System.out.println(3);
}
b.b = true || true;
if(!b.b) {
System.out.println(4);
}
b.b = true || false;
if(!b.b) {
System.out.println(5);
}
b.b = false && true;
if(!b.b) {
System.out.println(6);
}
b.b = true && true;
if(!b.b) {
System.out.println(7);
}
b.b = false && false;
if(!b.b) {
System.out.println(8);
}
b.b = true && false;
if(!b.b) {
System.out.println(9);
}
}
}
50
47
7040
3741
4502
1682
8636
4694
6347
685
5099
7104
7485
2296
9589
6862
5569
2599
578
1353
1326
2916
3558
19
3762
288
1168
6562
296
8648
5199
6495
5299
784
8934
3561
6345
4321
4841
6709
2268
7336
9524
6170
3555
5789
5238
2928
3959
5864
1553
10
9
8
7
6
5
4
3
2
1
0
class BubbleSort {
public int[] sortArray;
public int length;
public void setArray(int[] sortArray, int length) {
this.sortArray = sortArray;
this.length = length;
}
public int readInt() throws Exception {
int nextChar;
int sign = 1;
int number = 0;
int asciiZero = 48;
int asciiMinus = 45;
nextChar = System.in.read();
/* Check for sign */
if (nextChar == asciiMinus) {
sign = sign * (-1);
/* Advance */
nextChar = System.in.read();
}
/* CR/LF Characters */
while (nextChar != 10 && nextChar != 13) {
if (nextChar >= asciiZero && nextChar <= (asciiZero + 9)) {
/* Found a digit */
number = number * 10 + nextChar - asciiZero;
}
nextChar = System.in.read();
}
return sign * number;
}
public void sort() {
boolean swapped = true;
while (swapped) {
swapped = false;
int i = 0;
while (i <= this.length - 2) {
if (this.sortArray[i] > this.sortArray[i + 1]) {
this.swap(i, i + 1);
swapped = true;
}
i = i + 1;
}
}
}
public void swap(int i, int j) {
int temp = this.sortArray[i];
this.sortArray[i] = this.sortArray[j];
this.sortArray[j] = temp;
}
public static void main(String[] args) throws Exception {
BubbleSort sort = new BubbleSort();
int arrayLen = sort.readInt();
int[] array = new int[arrayLen];
int i = 0;
while(i < arrayLen) {
array[i] = sort.readInt();
i = i + 1;
}
sort.setArray(array, arrayLen);
sort.sort();
/* Print array */
i = 0;
while (i < sort.length) {
System.out.println(sort.sortArray[i]);
i = i + 1;
}
}
}
class Foo {
public int ret_int() {
return 48;
}
public static void main(String[] args) {
Foo a = new Foo();
int b = a.ret_int();
System.out.println(b);
}
}
class A {
public boolean side_effect() {
System.out.write(70); /* F */
System.out.write(10); /* newline */
return false;
}
public static void main(String[] args) {
A a = new A();
if (true || a.side_effect()) {
System.out.write(79); /* O */
System.out.write(75); /* K */
System.out.write(10); /* newline */
}
if (false && a.side_effect()) {
} else {
System.out.write(79); /* O */
System.out.write(75); /* K */
System.out.write(10); /* newline */
}
if (!(false && a.side_effect())) {
System.out.write(79); /* O */
System.out.write(75); /* K */
System.out.write(10); /* newline */
}
if ((true || a.side_effect()) && true) {
System.out.write(79); /* O */
System.out.write(75); /* K */
System.out.write(10); /* newline */
}
}
}
class EvaluationOrder {
public int i;
public EvaluationOrder setI(int i) {
this.i = i + 1;
System.out.println(this.i);
return this;}
public static void main(String[] args) {
EvaluationOrder e = new EvaluationOrder();
int[] arr = new int[3];
arr[e.setI(-1).i] = e.setI(e.setI(2).i).i + 4;
System.out.println(arr[0]);
}
}
\ No newline at end of file
class EvaluationOrder {
public int i;
public EvaluationOrder setI(int i) {
this.i = i + 1;
System.out.println(this.i);
return this;}
public static void main(String[] args) {
EvaluationOrder e = new EvaluationOrder();
int[] arr = new int[3];
arr[e.setI(-1).i] = e.setI(e.setI(2).i).i + e.setI(1).i * e.setI(2).i + e.setI(3).i / e.setI(4).i % e.setI(5).i;
System.out.println(arr[0]);
}
}
\ No newline at end of file
class Fib {
public int fib(int n) {
if(n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
Fib fib = new Fib();
System.out.println(fib.fib(24));
}
}
\ No newline at end of file
46368
\ No newline at end of file
import lib.BooleanUtils;
class Test {
public static void main(String[] args) {
new Test().test();
}
public boolean booleanField;
public int intField;
public Test objectField;
public Test[] arrayField;
public void test() {
booleanField = true;
intField = 123;
objectField = new Test();
objectField.booleanField = true;
arrayField = new Test[5];
arrayField[0] = new Test();
arrayField[0].booleanField = true;
BooleanUtils bu = new BooleanUtils();
bu.println(booleanField);
System.out.println(intField);
bu.println(objectField.booleanField);
bu.println(arrayField[0].booleanField);
}
}
class A {
public int a;
public void print_as() {
System.out.println(a);
System.out.println(this.a);
}
public static void main(String[] args) {
A a = new A();
a.print_as();
B b = new B();
if (b.b) {
System.out.println(1);
} else {
System.out.println(0);
}
if (b.c == null) {
System.out.println(0);
} else {
System.out.println(1);
}
}
}
class B {
public boolean b;
public A c;
}
class If {
public static void main(String[] args) {
boolean b = true;
if (b) {
b = false;
if (b) {
System.out.println(1);
} else {
if (!b) {
if (b) {
System.out.println(2);
} else {
System.out.println(3);
}
} else {
b = true;
if (b) {
System.out.println(4);
} else {
System.out.println(5);
}
}
}
} else {
System.out.println(6);
}
}
}
\ No newline at end of file
class Foo {
public static void main(String[] args) {
if (true) {
System.out.println(1);
} else {
System.out.println(0);
}
if (false) {
System.out.println(0);
} else {
System.out.println(1);
}
}
}
class A {
public static void main(String[] args) {
((System).out).println(4);
}
}
class Foo {
public Bar bar() {
return new Bar();
}
public static void main(String[] args) {
System.out.println(new Foo().bar().baz().x);
}
}
class Bar {
public Baz baz() {
Baz baz = new Baz();
baz.x = 42;
return baz;
}
}
class Baz {
public int x;
}
42
\ No newline at end of file
20
-1737
9169
7808
8896
-8482
9322
-943
-9196
-7588
-1390
5526
7096
264
-5848
-8359
-5300
-3034
-6372
1936
-7563
import lib.IntArrayUtils;
class InsertionSort {
public IntArray _array;
/* constructor */
public InsertionSort init(IntArray array) {
this._array = array;
return this;
}
public void sortArray() {
int i = 0;
while (i < _array.length) {
int j = i;
while (j > 0 && _array.get(j-1) > _array.get(j)) {
swapArrayElements(j, j-1);
j = j -1;
}
i = i + 1;
}
}
public void swapArrayElements(int indexA, int indexB) {
int tmp = _array.get(indexA);
_array.set(indexA, _array.get(indexB));
_array.set(indexB, tmp);
}
public IntArray getArray() {
return _array;
}
public static void main(String[] args) throws Exception {
IntArrayUtils iau = new IntArrayUtils();
IntArray arrayToSort = iau.readIntArrayFromStdin();
InsertionSort insertionSort = new InsertionSort().init(arrayToSort);
insertionSort.sortArray();
IntArray sortedArray = insertionSort.getArray();
iau.printIntArray(sortedArray);
}
}
10
1
2
3
4
5
6
7
8
9
10
3
-1
2147483647
-2147483648
import lib.IntArrayUtils;
class Test {
public static void main(String[] args) throws Exception {
IntArrayUtils intArrayUtils = new IntArrayUtils();
IntArray intArray = intArrayUtils.readIntArrayFromStdin();
intArrayUtils.printIntArray(intArray);
}
}
class A {
public B get_object() {
return new B();
}
public static void main(String[] args) {
A a = new A();
int res = (a.get_object()).get_number();
System.out.println(res);
}
}
class B {
public int get_number() {
return 99;
}
}
......@@ -10,6 +10,10 @@ public class BooleanUtils {
}
}
public boolean toBoolean(int value) {
return value != 0;
}
public BooleanUtils println(boolean value){
System.out.println(toInt(value));
return this;
......
package lib;
/*
* Utils to read int arrays from stdin and output them back to stdout.
*
* Methods using System.in.read() throw Exception to be valid Java code.
* java.io.IOException is the only exception thrown, but that is not a valid MiniJava IDENT.
*/
public class IntArrayUtils {
/* Reads a line as an int from stdin */
public int readIntFromStdin() throws Exception {
int result = 0;
int c = System.in.read();
boolean negative = false;