Last active
December 23, 2017 13:52
-
-
Save kevincharm/a11c1cdd2c8ee155099479fa184ca5a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- node.jz -*- | |
let input = '' | |
process.stdin.on('readable', () => input += process.stdin.read() || '') | |
process.stdin.on('end', () => main()) | |
const EventEmitter = require('events') | |
class Program extends EventEmitter { | |
constructor(id) { | |
super() | |
this.id = 0 | |
this.stack = {} | |
this.i = 0 | |
} | |
load(programStr) { | |
this.instructions = programStr | |
.split('\n') | |
.map(line => line.split(/\s/)) | |
} | |
next() { | |
let i = this.i | |
const [op, arg1, arg2] = this.instructions[i] | |
const valArg1 = this.stack[arg1] || parseInt(arg1) || 0 | |
const valArg2 = this.stack[arg2] || parseInt(arg2) || 0 | |
switch (op) { | |
case 'set': | |
this.stack[arg1] = valArg2 | |
i += 1 | |
break | |
case 'sub': | |
this.stack[arg1] = valArg1 - valArg2 | |
i += 1 | |
break | |
case 'mul': | |
this.emit('mul') | |
this.stack[arg1] = valArg1 * valArg2 | |
i += 1 | |
break | |
case 'jnz': | |
if (valArg1 !== 0) | |
i += valArg2 | |
else | |
i += 1 | |
break | |
} | |
this.i = i | |
} | |
run() { | |
while (this.i >= 0 && this.i < this.instructions.length) { | |
this.next() | |
} | |
this.emit('end') | |
} | |
} | |
function main() { | |
const zero = new Program(0) | |
let mulcount = 0 | |
zero.on('mul', () => { | |
mulcount++ | |
}) | |
zero.on('end', () => { | |
console.log(`Multiplies: ${mulcount}`) | |
}) | |
zero.load(input) | |
zero.run() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b = 57 | |
c = b | |
// jnz a 2 | |
// jnz 1 5 | |
b *= 100 | |
b -= -100000 | |
c = b | |
c -= -17000 | |
do { f = 1 | |
d = 2 | |
do { e = 2 | |
do { g = d | |
g *= e | |
g -= b | |
if (g === 0) | |
f = 0 | |
e -= -1 | |
g = e | |
g -= b | |
} while (g !== 0) | |
d -= -1 | |
g = d | |
g -= b | |
} while (g !== 0) | |
if (f === 0) | |
h -= -1 | |
g = b | |
g -= c | |
if (g === 0) | |
process.exit() | |
b -= -17 | |
} while (true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() { | |
int a = 1; | |
int b, c, d, e, f, g, h; | |
b = c = d = e = f = g = h = 0; | |
b = 105700; | |
c = b + 17000; | |
for (; b <= c; b += 17) { | |
f = 1; | |
for (d = 2; d <= b/2; d++) { | |
if (b % d == 0) { | |
f = 0; | |
} | |
} | |
if (f == 0) { | |
h += 1; | |
} | |
} | |
printf("h: %d\n", h); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment