Skip to content

Instantly share code, notes, and snippets.

@Gerst20051
Last active June 24, 2021 04:50
Show Gist options
  • Save Gerst20051/17ff98edb15183335be9fd5bc2663bed to your computer and use it in GitHub Desktop.
Save Gerst20051/17ff98edb15183335be9fd5bc2663bed to your computer and use it in GitHub Desktop.
Calculate Complex
const expression1 = '5+16-((9-6)-(4-2))+1';
const expression2 = '22+(2-4)';
const expression3 = '6+9-12';
const expression4 = '((1024))';
const expression5 = '1+(2+3)-(4-5)+6';
const expression6 = '255';
const operations1 = [['5', '+', '16', '-'], [['9', '-', '6'], '-', ['4', '-', '2']], ['+', '1']];
const operations2 = [['22', '+'], ['2', '-', '4']];
const operations3 = [['6', '+', '9', '-', '12']];
const operations4 = [[[['1024']]]];
const operations5 = [['1', '+'], ['2', '+', '3'], ['-'], ['4', '-', '5'], ['+', '6']];
const operations6 = [['255']];
function calculate(input) {
const operations = buildOperations(input);
return reduceOperations(operations);
}
// 1+(2+3)-(4-5)+6
// VERSION 1: [[['1', '+', '2', '+', '3', '-', '4', '-', '5', '+', '6']]]
// CURRENT: [[ '1', '+' ], [ '2', '+', '3', '-' ], [ '4', '-', '5', '+', '6' ]]
// TARGET: [[['1', '+'], ['2', '+', '3'], ['-'], ['4', '-', '5'], ['+', '6']]]
function buildOperations(input) {
const characters = input.split('');
const operations = [];
let depths = [];
const operators = ['+', '-'];
let currentOperation = operations[operations.length - 1];
let currentOperationPart = currentOperation ? currentOperation[currentOperation.length - 1] : undefined;
for (let characterIndex = 0; characterIndex < characters.length; characterIndex++) {
const character = characters[characterIndex];
console.log(`DEPTHS ${character}`, depths);
if (character === '(') {
if (depths.length === 0) {
operations.push([]);
} else {
for (let depth of depths) {
console.log('LOOP DEPTH', depth);
// currentOperation = currentOperation[];
// currentOperationPart = currentOperation ? currentOperation[currentOperation.length - 1] : undefined;
}
// operations[depths[depths.length - 1]].push([]);
}
// if (depths.length === 0) {
// depths.push(operations.length - 1);
// } else {
// }
} else if (character === ')') {
depths.pop();
if (depths.length === 0) {
operations.push([]);
} else {
}
} else {
if (depths.length === 0) {
if (!currentOperation) {
operations.push([]);
currentOperation = operations[operations.length - 1];
currentOperationPart = currentOperation ? currentOperation[currentOperation.length - 1] : undefined;
}
if (operators.includes(character) || !currentOperationPart || operators.includes(currentOperationPart)) {
currentOperation.push(character);
} else {
currentOperation[currentOperation.length - 1] += character;
}
} else {
}
}
}
console.log(input, operations);
return operations;
}
function reduceOperations(operations) {
const reduced = operations.map(operation => {
operation = operation.map(part => Array.isArray(part) ? reduceOperations([part]) : part);
while (operation.length >= 3) {
operation = [runCalculation(operation.slice(0, 3)), ...operation.slice(3)];
}
return operation;
});
return reduced[0].length > 1 ? reduceOperations([reduced.flat()]) : parseInt(reduced[0][0]);
}
function runCalculation(calculation) {
const operator = calculation[1];
const firstOperand = parseInt(calculation[0], 10);
const secondOperand = parseInt(calculation[2], 10);
if (operator === '+') return firstOperand + secondOperand;
if (operator === '-') return firstOperand - secondOperand;
if (operator === '*') return firstOperand * secondOperand;
if (operator === '/') return firstOperand / secondOperand;
console.error(calculation);
}
console.log('REDUCE OPERATIONS');
// console.log(reduceOperations(operations1) === 21);
// console.log(reduceOperations(operations2) === 20);
// console.log(reduceOperations(operations3) === 3);
// console.log(reduceOperations(operations4) === 1024);
// console.log(reduceOperations(operations5) === 13);
// console.log(reduceOperations(operations6) === 255);
console.log('');
console.log('BUILD OPERATIONS');
// console.log(JSON.stringify(buildOperations(expression1)) === JSON.stringify(operations1));
// // console.log(JSON.stringify(buildOperations(expression2)) === JSON.stringify(operations2));
// // console.log(JSON.stringify(buildOperations(expression3)) === JSON.stringify(operations3));
// console.log(JSON.stringify(buildOperations(expression4)) === JSON.stringify(operations4));
console.log(JSON.stringify(buildOperations(expression5)) === JSON.stringify(operations5));
// console.log(JSON.stringify(buildOperations(expression6)) === JSON.stringify(operations6));
console.log('');
console.log('CALCULATE');
// // console.log(calculate(expression1) === 21);
// console.log(calculate(expression2) === 20);
// console.log(calculate(expression3) === 3);
// // console.log(calculate(expression4) === 1024);
// // console.log(calculate(expression5) === 13);
// console.log(calculate(expression6) === 255);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment