Skip to content

Commit

Permalink
🚂 refactored challenge 22: programming language
Browse files Browse the repository at this point in the history
  • Loading branch information
jamerrq committed Dec 22, 2023
1 parent 6e603a3 commit 3010c0b
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/challenges/22.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ export function compile (code: string): number {
let inside = null
const instructions = code.split('')
while (index < instructions.length) {
const outside = inside !== false
const instruction = instructions[index]
if (instruction === '+') {
ans += 1 * +(inside === null || inside)
} else if (instruction === '-') {
ans -= 1 * +(inside === null || inside)
} else if (instruction === '*') {
ans *= 2 * +(inside === null || inside)
} else if (instruction === '%') {
last = [last, index][+(inside === null || inside)]
} else if (instruction === '<') {
instructions[index] = ' '
index = [last - 1, index - 1][+(last === -1)]
}
ans += +(outside) * +(instruction === '+')
ans -= 1 * +(outside) * +(instruction === '-')
ans *= [1, 2][+(outside) * +(instruction === '*')]
last = [last, index][+(outside) * +(instruction === '%')]
const a = +(instruction === '¿')
const b = +(ans > 0)
const c = +(instruction === '?')
inside = [inside, null, true, false][2 * a + c + (1 - b) * (1 - c) * (a + b)]
inside = [inside, null, true, false][
2 * a + c + (1 - b) * (1 - c) * (a + b)
]
const prev = index
index = [index, last - 1][+(last !== -1) * +(instructions[index] === '<')]
instructions[prev] = [instructions[index], ' '][+(instruction === '<')]
index++
}
return ans
Expand Down

0 comments on commit 3010c0b

Please sign in to comment.