Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. var fs = require('fs');
  2. var myInputs = fs.readFileSync('d7input.txt').toString().split("\r\n");
  3. var linesrun = 0;
  4.  
  5. function lim16(l){ //16 bit 'implementation'; cuts stuff to 16-bitty size
  6. return (l & 0xFFFF);
  7. }
  8.  
  9. function aocLog(inputvars, operand, nr, outputvar){//array of string, string, int, string
  10. this.inputvars = inputvars;
  11. this.operand = operand;
  12. this.outputvar = outputvar;
  13. this.nr = nr;
  14. this.unexec = true; //unexecuted yet
  15.  
  16. this.tryRunOp = function(dictIn){
  17. if(this.unexec){
  18. let mrun = true;
  19. for (const invar of inputvars) { //are variables in the dictionary?
  20. if(dictIn[invar] === undefined){ //if it's not in there
  21. mrun = false; //don't let it run
  22. }
  23. }
  24. if(mrun){
  25. this.unexec = false; //this has run to this point, variables are known
  26. linesrun++;
  27. //lim16 on each function call is not entirely necessary, but i'm doing it just to avoid potential errors
  28. switch(operand){
  29. case "STO":
  30. //directly save value
  31. if(this.inputvars.length){ //are there input vars?
  32. dictIn[this.outputvar] = lim16(dictIn[this.inputvars[0]]);
  33. } else{
  34. dictIn[this.outputvar] = lim16(this.nr);
  35. }
  36. break;
  37. case "NOT":
  38. //one variable, not
  39. dictIn[this.outputvar] = lim16(dictIn[this.inputvars[0]] ^ 0xFFFF);
  40. break;
  41. case "AND": //ands can happen with numbers, be careful, nested switch
  42. switch(this.inputvars.length){
  43. case 1:
  44. //one variable, AND with a number
  45. dictIn[this.outputvar] = lim16(dictIn[this.inputvars[0]] & this.nr);
  46. break;
  47. case 2:
  48. //two variables, AND with 2 variables
  49. dictIn[this.outputvar] = lim16(dictIn[this.inputvars[0]] & dictIn[this.inputvars[1]]);
  50. break;
  51. }
  52. break;
  53. case "OR":
  54. dictIn[this.outputvar] = lim16(dictIn[this.inputvars[0]] | dictIn[this.inputvars[1]]);
  55. break;
  56. case "LSHIFT":
  57. dictIn[this.outputvar] = lim16(dictIn[this.inputvars[0]] << this.nr);
  58. break;
  59. case "RSHIFT":
  60. dictIn[this.outputvar] = lim16(dictIn[this.inputvars[0]] >> this.nr);
  61. break;
  62. }
  63. }
  64. }
  65. }
  66. }
  67.  
  68.  
  69. var resultDict = {}; //using an object as a 'dictionary'
  70.  
  71. var logOps = []; //array to contain aocLog objects
  72. //function to populate objects
  73. //parses the seperated lines into an array of objects to iterate through
  74. for (line of myInputs) { //iterates through and puts in objects
  75. sL = line.split(" ");
  76. // console.log(sL);
  77. //first check if it's a NOT operation
  78. switch(sL.length){ //length to seperate options
  79. case 3: //it's a STO
  80. let tmpval = Number(sL[0]);
  81. if(tmpval || tmpval === 0){ //a number gets stored, even if it's 0
  82. logOps.push(new aocLog([],"STO",tmpval,sL[2]));
  83. } else{ //a var gets transmitted to another var
  84. logOps.push(new aocLog([sL[0]],"STO",0,sL[2]));
  85. }
  86. break;
  87. case 4: //it's a NOT
  88. logOps.push(new aocLog([sL[1]],"NOT", 0, sL[3])); //stores a not object
  89. break;
  90. case 5: //it's AND, OR or (L|R)Shift
  91. switch(sL[1]){
  92. case "AND":
  93. let tmpval = Number(sL[0]);
  94. if(tmpval){
  95. logOps.push(new aocLog([sL[2]],"AND",tmpval,sL[4])); //it's an AND with a number
  96. //attention: this only works if the first operand is a number; as nothing else appears in my
  97. //data that's fine, otherwise you'd need to check both.
  98. }else{
  99. logOps.push(new aocLog([sL[0],sL[2]],"AND",0,sL[4])); //it's an AND with 2 funcs
  100. }
  101. break;
  102. case "OR":
  103. logOps.push(new aocLog([sL[0],sL[2]],"OR",0,sL[4])); //stores an OR
  104. break;
  105. case "LSHIFT":
  106. logOps.push(new aocLog([sL[0]],"LSHIFT",Number(sL[2]),sL[4]));
  107. break;
  108. case "RSHIFT":
  109. logOps.push(new aocLog([sL[0]],"RSHIFT",Number(sL[2]),sL[4]));
  110. break;
  111. }
  112. break;
  113.  
  114. }
  115. }
  116. //runs through all lines until everything is run:
  117. while(linesrun < logOps.length){
  118. for (logOp of logOps) {
  119. logOp.tryRunOp(resultDict);
  120. }
  121. console.log(linesrun);
  122. }
  123.  
  124. console.log(resultDict);
  125. console.log("a is: " + resultDict["a"]);
  126.  
  127.  
  128. // //test code to see if my operations work
  129. // resultDict["a"] = 3;
  130. // resultDict["b"] = 1;
  131. // resultDict["c"] = 4;
  132. // testOb1 = new aocLog(["b"],"STO",0,"stoTest"); //store b in c
  133. // testOb2 = new aocLog(["b"],"NOT",0,"notTest"); //nottest, result has to be 65534
  134. // testOb3 = new aocLog(["b","a"],"AND",0,"andTest"); //and test, result has to be 1
  135. // testOb4 = new aocLog(["b","c"],"OR",0,"orTest"); //or test, result has to be 5
  136. // testOb5 = new aocLog(["a"],"LSHIFT",2,"lShift"); //lshifttest, result has to be 3*4 = 12
  137. // testOb6 = new aocLog(["c"],"RSHIFT",1,"rShift"); //rshifttest, result has to be 2
  138.  
  139. // testOb1.tryRunOp(resultDict);
  140. // testOb2.tryRunOp(resultDict);
  141. // testOb3.tryRunOp(resultDict);
  142. // testOb4.tryRunOp(resultDict);
  143. // testOb5.tryRunOp(resultDict);
  144. // testOb6.tryRunOp(resultDict);
  145.  
  146. // console.log(resultDict);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement