2_2.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const Computer = require("../IntComp/Computer");
  2. const input = [1, 12, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 13, 19, 1, 9, 19, 23, 1, 6, 23, 27, 2, 27, 9, 31, 2, 6, 31, 35, 1, 5, 35, 39, 1, 10, 39, 43, 1, 43, 13, 47, 1, 47, 9, 51, 1, 51, 9, 55, 1, 55, 9, 59, 2, 9, 59, 63, 2, 9, 63, 67, 1, 5, 67, 71, 2, 13, 71, 75, 1, 6, 75, 79, 1, 10, 79, 83, 2, 6, 83, 87, 1, 87, 5, 91, 1, 91, 9, 95, 1, 95, 10, 99, 2, 9, 99, 103, 1, 5, 103, 107, 1, 5, 107, 111, 2, 111, 10, 115, 1, 6, 115, 119, 2, 10, 119, 123, 1, 6, 123, 127, 1, 127, 5, 131, 2, 9, 131, 135, 1, 5, 135, 139, 1, 139, 10, 143, 1, 143, 2, 147, 1, 147, 5, 0, 99, 2, 0, 14, 0];
  3. const demos = [
  4. [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50],
  5. [1, 0, 0, 0, 99],
  6. [2, 3, 0, 3, 99],
  7. [2, 4, 4, 5, 99, 0],
  8. [1, 1, 1, 4, 99, 5, 6, 0, 99],
  9. ];
  10. function CalculateNounPlusVerb(noun, verb, expected) {
  11. const freshMemory = JSON.parse(JSON.stringify(input));
  12. freshMemory[1] = noun;
  13. freshMemory[2] = verb;
  14. const comp = new Computer(freshMemory);
  15. comp.Run();
  16. return comp.stack.GetAtIndex(0, 1) == expected;
  17. }
  18. function LoopNounsAndVerbs() {
  19. const EXPECTED = 19690720;
  20. let noun = 0;
  21. let verb = 0;
  22. for (noun = 0; noun < 100; noun++) {
  23. for (verb = 0; verb < 100; verb++) {
  24. if (CalculateNounPlusVerb(noun, verb, EXPECTED)) {
  25. return 100 * noun + verb;
  26. }
  27. }
  28. }
  29. return false;
  30. }
  31. const output = LoopNounsAndVerbs();
  32. if (output !== false) {
  33. console.log(`Answer: ${output}`);
  34. }
  35. else {
  36. console.log("Couldn't find answer");
  37. }
  38. // computer.DumpMemory();