Last active
October 30, 2016 16:10
-
-
Save tiago/6f3530a5a68dd4f4aa2e1e329b48c6f4 to your computer and use it in GitHub Desktop.
Pixels Camp Challenge #2 - Final submission
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
| c = (program, register, undefined) => { | |
| // These variable declarations are just to aid compression and should be removed from the compressed output | |
| var memory, instruction_pointer, memory_pointer, write_char, level, commands, instruction, tokens, instructions, find_matching, memory_value; | |
| memory = [instruction_pointer = memory_pointer = 0]; | |
| write_char = output_char; | |
| find_matching = (start, stop, direction) => { | |
| level = 1; | |
| instruction_pointer += direction; | |
| while(level += instructions[instruction_pointer += direction] == start ? 1 : instructions[instruction_pointer] == stop ? -1 : 0); | |
| } | |
| commands = { | |
| moo: $ => find_matching(tokens[0], tokens[7], -1) | instruction_pointer--, | |
| mOo: $ => memory_pointer--, | |
| moO: $ => memory[++memory_pointer] |= 0, | |
| mOO: $ => memory_value == 3 || commands[tokens[memory_value]](), | |
| Moo: $ => memory_value ? write_char(String.fromCharCode(memory_value)) : memory[memory_pointer] = input_char(), | |
| MOo: $ => memory[memory_pointer]--, | |
| MoO: $ => memory[memory_pointer]++, | |
| MOO: $ => memory_value || find_matching(tokens[7], tokens[0], 1), | |
| OOO: $ => memory[memory_pointer] = 0, | |
| MMM: $ => register ? (memory[memory_pointer] = register, register = undefined) : register = memory_value, | |
| OOM: $ => [...memory_value+''].map(write_char) | |
| }; | |
| tokens = Object.keys(commands); | |
| instructions = program.match(RegExp(tokens.join`|`, 'g')); | |
| while (instruction = instructions[instruction_pointer]) { | |
| commands[instruction](memory_value = memory[memory_pointer]); | |
| instruction_pointer++; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment