Advertisement
imsalahdev

Advent Of Code - Day 5 - improved code

Dec 5th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const path = require("path");
  2. const fs = require("fs");
  3.  
  4. const input = fs.readFileSync(
  5.     path.resolve(__dirname, "part2_input.txt"),
  6.     "UTF-8"
  7. );
  8.  
  9. class Computer {
  10.     constructor(program, systemID) {
  11.         this.program = program;
  12.         this.systemID = systemID;
  13.         this.pointer = 0;
  14.         this.programOutput = null;
  15.     }
  16.  
  17.     parseCommand() {
  18.         const instruction = this.program[this.pointer].toString();
  19.         const [E, D = "0", C = "0", B = "0", A = "0"] = [...instruction].reverse();
  20.         return {
  21.             opcode: Number(D + E).toString(),
  22.             modes: { A, B, C }
  23.         };
  24.     }
  25.  
  26.     operations(opcode) {
  27.         return {
  28.             "1": ([a, b, o] = []) => { // ADD
  29.                 this.program[o] = a + b;
  30.                 this.pointer += 4;
  31.             },
  32.             "2": ([a, b, o] = []) => { // MULT
  33.                 this.program[o] = a * b;
  34.                 this.pointer += 4;
  35.             },
  36.             "3": () => { // INPUT
  37.                 const address = this.program[this.pointer + 1];
  38.                 this.program[address] = this.systemID;
  39.                 this.pointer += 2;
  40.             },
  41.             "4": () => { // OUTPUT
  42.                 const address = this.program[this.pointer + 1];
  43.                 this.programOutput = this.program[address];
  44.                 this.pointer += 2;
  45.             },
  46.             "5": ([a, b] = []) => { // JUMP IF TRUE
  47.                 this.pointer += 3;
  48.                 if (a !== 0) {
  49.                     this.pointer = b;
  50.                 }
  51.             },
  52.             "6": ([a, b] = []) => { // JUMP IF FALSE
  53.                 this.pointer += 3;
  54.                 if (a === 0) {
  55.                     this.pointer = b;
  56.                 }
  57.             },
  58.             "7": ([a, b, o] = []) => { // LESS THAN
  59.                 this.program[o] = (a < b) ? 1 : 0;
  60.                 this.pointer += 4;
  61.             },
  62.             "8": ([a, b, o] = []) => { // EQUALS
  63.                 this.program[o] = (a === b) ? 1 : 0;
  64.                 this.pointer += 4;
  65.             },
  66.             "99": () => -1 // HALT
  67.         }[opcode];
  68.     }
  69.  
  70.     getValueByMode(input, mode) {
  71.         return (mode === "0") ? this.program[input] : input;
  72.     }
  73.  
  74.     parseParameters({A, B, C} = {}) {
  75.         let [a, b, o] = this.program.slice(this.pointer + 1, this.pointer + 4);
  76.         a = this.getValueByMode(a, C);
  77.         b = this.getValueByMode(b, B);
  78.         return [a, b, o];
  79.     }
  80.  
  81.     checkOperation(operation, opcode) {
  82.         if(operation === undefined) {
  83.             throw new Error(`unknown opcode: ${opcode}`);
  84.         }
  85.     }
  86.  
  87.     run() {
  88.         while (true) {
  89.             const { opcode, modes } = this.parseCommand();
  90.             const operation = this.operations(opcode);
  91.             const parameters = this.parseParameters(modes);
  92.            
  93.             this.checkOperation(operation, opcode);
  94.  
  95.             const code = operation(parameters);
  96.            
  97.             if (code === -1) break;
  98.         }
  99.         return this.programOutput;
  100.     }
  101. }
  102.  
  103.  
  104.  
  105. const program = input.split(",").map(Number);
  106. const systemID = 5;
  107. const computer = new Computer(program, systemID);
  108. const output = computer.run();
  109. console.log(output);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement