Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let relativeBase = 0;
- let instrus = [];
- const intcode = (program, inputs, index) => {
- const s = program.split`,`.map(n => parseInt(n));
- let stop = false;
- instrus = [];
- while (index < s.length && !stop) {
- const instructions = s.slice(index, index + 4);
- const opcode = instructions[0] % 100;
- const modes = instructions[0].toString().padStart(5, "0");
- const getParameter = position => {
- const positionMode = modes[3 - position];
- if (positionMode == "0") {
- // console.log(i, position, modes, positionMode, instructions, instructions[position], s[instructions[position]]);
- return s[instructions[position]];
- } else if (positionMode == "1") {
- // console.log(i, position, modes, positionMode, instructions, instructions[position]);
- return instructions[position];
- } else {
- return s[relativeBase + instructions[position]];
- }
- };
- const getPosition = position => {
- const positionMode = modes[3 - position];
- if (positionMode === "0") {
- return instructions[position];
- } else if (positionMode == "1") {
- console.log("ok");
- } else {
- return relativeBase + instructions[position];
- }
- };
- // console.log(i, opcode, instructions);
- switch (opcode) {
- case 1:
- s[getPosition(3)] = getParameter(1) + getParameter(2);
- // console.log('opcode 1', instructions[3], s[instructions[3]]);
- index += 4;
- break;
- case 2:
- s[getPosition(3)] = getParameter(1) * getParameter(2);
- // console.log('opcode 2', instructions[3], s[instructions[3]]);
- index += 4;
- break;
- case 3:
- s[getPosition(1)] = inputs.shift();
- // console.log('opcode 3', instructions[1], s[instructions[1]]);
- index += 2;
- break;
- case 4:
- output = getParameter(1);
- instrus.push(output);
- index += 2;
- if (instrus.length === 2) {
- return [s.join(","), index, false];
- }
- // console.log('opcode 4');
- break;
- case 5:
- if (getParameter(1) !== 0) {
- index = getParameter(2);
- } else {
- index += 3;
- }
- break;
- case 6:
- if (getParameter(1) === 0) {
- index = getParameter(2);
- } else {
- index += 3;
- }
- break;
- case 7:
- if (getParameter(1) < getParameter(2)) {
- s[getPosition(3)] = 1;
- } else {
- s[getPosition(3)] = 0;
- }
- index += 4;
- break;
- case 8:
- if (getParameter(1) === getParameter(2)) {
- s[getPosition(3)] = 1;
- } else {
- s[getPosition(3)] = 0;
- }
- index += 4;
- break;
- case 9:
- param = getParameter(1);
- relativeBase += param;
- index += 2;
- break;
- case 99:
- stop = true;
- return [s.join(","), index, true];
- break;
- default:
- console.log(
- "FAILURE WRONG OPCODE AT POSITION",
- index,
- ":",
- instructions[0]
- );
- stop = true;
- break;
- }
- // console.log(s.slice(0,50).join`,`);
- }
- return s[0];
- };
- let program =
- "3,8,1005,8,339,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,29,2,1108,11,10,1,1,20,10,2,107,6,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,62,1006,0,29,1006,0,12,1,1101,5,10,1,2,20,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,99,1006,0,30,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,124,1006,0,60,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,149,2,1007,2,10,1,1105,10,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,178,1,1108,15,10,1,1101,5,10,1,109,8,10,1006,0,20,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,215,1006,0,61,1006,0,16,2,1105,15,10,1006,0,50,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,250,1,1003,10,10,1,9,19,10,2,1004,6,10,2,1106,2,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,289,1,1103,13,10,2,105,17,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,318,101,1,9,9,1007,9,1086,10,1005,10,15,99,109,661,104,0,104,1,21101,0,825599304340,1,21101,356,0,0,1106,0,460,21101,0,937108545948,1,21102,1,367,0,1106,0,460,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,1,21628980315,1,21101,0,414,0,1105,1,460,21101,0,3316673539,1,21101,425,0,0,1106,0,460,3,10,104,0,104,0,3,10,104,0,104,0,21102,988753428840,1,1,21102,1,448,0,1106,0,460,21102,825544569700,1,1,21102,459,1,0,1106,0,460,99,109,2,21202,-1,1,1,21102,1,40,2,21102,491,1,3,21102,481,1,0,1105,1,524,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,486,487,502,4,0,1001,486,1,486,108,4,486,10,1006,10,518,1101,0,0,486,109,-2,2105,1,0,0,109,4,2102,1,-1,523,1207,-3,0,10,1006,10,541,21102,0,1,-3,21201,-3,0,1,22102,1,-2,2,21102,1,1,3,21102,560,1,0,1106,0,565,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,588,2207,-4,-2,10,1006,10,588,22101,0,-4,-4,1105,1,656,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21102,1,607,0,1106,0,565,22102,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,626,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,648,21202,-1,1,1,21101,0,648,0,105,1,523,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0";
- let visited = [];
- let white = [];
- let black = [];
- let index = 0;
- let halt = false;
- let direction = 0; // 0 = up, 1 = left, 2 = down, 3 = right
- let y = 0;
- let x = 0;
- let count = 0;
- while (!halt) {
- if (whiteCheck([x, y])) {
- [program, index, halt] = intcode(program, [1], index);
- } else {
- [program, index, halt] = intcode(program, [0], index);
- }
- console.log(instrus + " : " + index + " => " + x + ", " + y);
- if (instrus[1] == 0) {
- direction = (direction + 1) % 4;
- } else if (instrus[1] == 1) {
- if (direction == 0) {
- direction += 4;
- }
- direction = direction - 1;
- } else {
- break;
- }
- paint([x, y], instrus[0]);
- switch (Math.abs(direction)) {
- case 0:
- y++;
- break;
- case 1:
- x--;
- break;
- case 2:
- y--;
- break;
- case 3:
- x++;
- break;
- default:
- console.log("error");
- }
- }
- console.log(visited.length);
- console.log(count);
- function paint(coord, color) {
- count++;
- if (!visitedcheck(coord)) {
- visited.push(coord);
- }
- if (color == 0) {
- if (!blackCheck(coord)) {
- black.push(coord);
- }
- if (whiteCheck(coord)) {
- white.splice(white.indexOf(coord), 1);
- }
- } else if (color == 1) {
- if (!whiteCheck(coord)) {
- white.push(coord);
- }
- if (blackCheck(coord)) {
- black.splice(black.indexOf(coord), 1);
- }
- } else {
- console.log("errorr");
- }
- }
- function whiteCheck(blockCheck) {
- var bol = false;
- white.forEach(element => {
- if (element[0] === blockCheck[0] && element[1] === blockCheck[1]) {
- bol = true;
- }
- });
- return bol;
- }
- function blackCheck(blockCheck) {
- var bol = false;
- black.forEach(element => {
- if (element[0] === blockCheck[0] && element[1] === blockCheck[1]) {
- bol = true;
- }
- });
- return bol;
- }
- function visitedcheck(blockCheck) {
- var bol = false;
- visited.forEach(element => {
- if (element[0] === blockCheck[0] && element[1] === blockCheck[1]) {
- bol = true;
- }
- });
- return bol;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement