Skip to content

Instantly share code, notes, and snippets.

@choyan
Created September 28, 2021 07:23
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 choyan/97bc8f4dcbf2989bd05259cf882aa5c2 to your computer and use it in GitHub Desktop.
Save choyan/97bc8f4dcbf2989bd05259cf882aa5c2 to your computer and use it in GitHub Desktop.
const list = [
1, 12, 2, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 9, 19, 1, 19, 5, 23, 2,
23, 13, 27, 1, 10, 27, 31, 2, 31, 6, 35, 1, 5, 35, 39, 1, 39, 10, 43, 2, 9,
43, 47, 1, 47, 5, 51, 2, 51, 9, 55, 1, 13, 55, 59, 1, 13, 59, 63, 1, 6, 63,
67, 2, 13, 67, 71, 1, 10, 71, 75, 2, 13, 75, 79, 1, 5, 79, 83, 2, 83, 9, 87,
2, 87, 13, 91, 1, 91, 5, 95, 2, 9, 95, 99, 1, 99, 5, 103, 1, 2, 103, 107, 1,
10, 107, 0, 99, 2, 14, 0, 0,
];
const chunkSize = 4;
const makeChunkedList = (arr, chunkSize) => {
const returnArr = [];
for (let i = 0, len = arr.length; i < len; i += chunkSize)
returnArr.push(arr.slice(i, i + chunkSize));
return returnArr;
};
function processArray(list) {
const updatedList = makeChunkedList(list, 4);
for (let i = 0; i < updatedList.length; i++) {
if (updatedList[i][0] === 99) {
list[0] = updatedList[i - 1][3];
updatedList[0][0] = updatedList[i - 1][3];
break;
} else {
if (updatedList[i][0] === 1) {
list[(i + 1) * 4 - 1] =
list[updatedList[i][1]] + list[updatedList[i][2]];
updatedList[i][3] = list[updatedList[i][1]] + list[updatedList[i][2]];
} else if (updatedList[i][0] === 2) {
list[(i + 1) * 4 - 1] =
list[updatedList[i][1]] * list[updatedList[i][2]];
updatedList[i][3] = list[updatedList[i][1]] * list[updatedList[i][2]];
}
}
}
return list[0];
}
function findOtherTwos(list) {
for (let i = 0; i < 100; i++) {
for (let j = 0; j < 100; j++) {
list[1] = j;
list[2] = i;
let output = processArray(list);
if (output === 19690720) {
console.log(`For output ${output} the two inputs are ${j} and ${i}`);
console.log(100 * j + i);
break;
}
}
}
}
let finalResult = processArray(list);
console.log("Final result is: ", finalResult);
findOtherTwos(list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment