Skip to content

Instantly share code, notes, and snippets.

@tonilopezmr
Last active March 29, 2022 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonilopezmr/6239520abdf7068207c7391285d4a231 to your computer and use it in GitHub Desktop.
Save tonilopezmr/6239520abdf7068207c7391285d4a231 to your computer and use it in GitHub Desktop.
//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,
print
]
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