From 3010c0bee1d85e2d92f3a02055e38fd80c87f97b Mon Sep 17 00:00:00 2001 From: jamerrq Date: Fri, 22 Dec 2023 14:34:16 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=82=20refactored=20challenge=2022:=20p?= =?UTF-8?q?rogramming=20language?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/challenges/22.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/challenges/22.ts b/src/challenges/22.ts index eeae284..65677e0 100644 --- a/src/challenges/22.ts +++ b/src/challenges/22.ts @@ -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