My solution in JS.
const operations = { 1: (inputArray, pos1, pos2, resultPos) => inputArray[inputArray[resultPos]] = inputArray[inputArray[pos1]] + inputArray[inputArray[pos2]], 2: (inputArray, pos1, pos2, resultPos) => inputArray[inputArray[resultPos]] = inputArray[inputArray[pos1]] * inputArray[inputArray[pos2]], }; const part1Solution = (inputArray, index = 0) => { const operation = inputArray[index]; if (operation === 99) { return inputArray[0]; } if (operation === 1 || operation === 2) { operations[operation](inputArray, index + 1, index + 2, index + 3) } else { throw new Error('Something went wrong!'); } return part1Solution(inputArray, index + 4); }; const part2Solution = (inputArray, targetValue) => { for (let i = 0; i <= 99; i ++) { for (let j = 0; j <= 99; j++) { const newArr = [...inputArray]; newArr[1] = i; newArr[2] = j; const currentResult = part1Solution(newArr); console.log('CURRENT VALUE', currentResult); if (currentResult === targetValue) { return 100 * i + j; } } } throw new Error('Value not found'); };
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My solution in JS.