Last active
March 29, 2022 10:13
-
-
Save tonilopezmr/6239520abdf7068207c7391285d4a231 to your computer and use it in GitHub Desktop.
This file contains 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
//Challenge https://github.com/jesus-seijas-sp/hand-challenge | |
// Command: node script.js input.hand | |
let helloworld = "ππππππππππ€πππππππππππππ€ππππππππ€ππ€ππππππππππ€ππ€ππππππππππππ€πππ€ππππππππππππππππππππππππππ€ππ€ππππ€πππ€πππππππππππππππππππππππππππππππππππππππππππππ" | |
const fs = require('fs') | |
var input = Array.from(helloworld) | |
try { | |
input = Array.from(fs.readFileSync(process.argv[2], 'utf8')) | |
} catch (err) { | |
console.error(err) | |
} | |
const nextPointer = (input, state) => { | |
if (input === "π") { | |
//console.log("π next cell") | |
state.memory.push(0) | |
state.index = state.index + 1 | |
return state | |
} | |
return state | |
} | |
const previousPointer = (input, state) => { | |
if (input === "π") { | |
//console.log("π previous cell") | |
state.index = state.index - 1 | |
return state | |
} | |
return state | |
} | |
const increment = (input, state) => { | |
if (input === "π") { | |
//console.log("π increment") | |
if(state.memory[state.index] === 255) { | |
state.memory[state.index] = 0 | |
} else { | |
state.memory[state.index] = state.memory[state.index] + 1 | |
} | |
return state | |
} | |
return state | |
} | |
const decrement = (input, state) => { | |
if (input === "π") { | |
//console.log("π decrement") | |
if(state.memory[state.index] === 0) { | |
state.memory[state.index] = 255 | |
} else { | |
state.memory[state.index] = state.memory[state.index] - 1 | |
} | |
return state | |
} | |
return state | |
} | |
const jump = (input, state) => { | |
if (input === "π€") { | |
//console.log("π€ Jump next") | |
if (state.memory[state.index] === 0) { | |
const newIndex = lookForJump(state.readerIndex+1, state.program, "π€", "π€") | |
state.readerIndex = newIndex + 1 | |
} | |
return state | |
} | |
return state | |
} | |
const jumpBack = (input, state) => { | |
if (input === "π€") { | |
//console.log("π€ Jump back") | |
if (state.memory[state.index] !== 0) { | |
const newIndex = lookForJumpBack(state.readerIndex-1, state.program, "π€", "π€") | |
state.readerIndex = newIndex + 1 | |
} | |
return state | |
} | |
return state | |
} | |
const print = (input, state) => { | |
if (input === "π") { | |
//console.log("π print: ", String.fromCharCode(state.memory[state.index])) | |
state.output.push(String.fromCharCode(state.memory[state.index])) | |
state.memory = state.memory.slice(0, state.index + 1) | |
return state | |
} | |
return state | |
} | |
const lookForJump = (index, input, element, skip) => { | |
let nestedSkip = 0 | |
for (let i = index; i < input.length; i++) { | |
const item = input[i] | |
if(item === element) { | |
if(nestedSkip > 0) { | |
nestedSkip-- | |
} else { | |
return i | |
} | |
} | |
if(item === skip) { | |
nestedSkip++ | |
} | |
} | |
return index | |
} | |
const lookForJumpBack = (index, input, element, skip) => { | |
let nestedSkip = 0 | |
for (let i = index; i >= 0; i--) { | |
const item = input[i] | |
if(item === element) { | |
if(nestedSkip > 0) { | |
nestedSkip-- | |
} else { | |
return i | |
} | |
} | |
if(item === skip) { | |
nestedSkip++ | |
} | |
} | |
return index | |
} | |
var state = { | |
program: input, | |
memory: [0], | |
index: 0, | |
readerIndex: 0, | |
output: [] | |
} | |
var commands = [ | |
nextPointer, | |
previousPointer, | |
increment, | |
decrement, | |
jump, | |
jumpBack, | |
] | |
while (state.readerIndex < input.length) { | |
const element = input[state.readerIndex] | |
const initReader = state.readerIndex | |
state = commands.reduce((previous, current) => current(element, previous), state) | |
if(initReader === state.readerIndex) { | |
state.readerIndex = state.readerIndex + 1 | |
} | |
} | |
console.log() | |
console.log(state.output.join("")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment