11_1.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const util = require("util");
  2. const Computer = require("../IntComp/Computer");
  3. const input = [3, 8, 1005, 8, 330, 1106, 0, 11, 0, 0, 0, 104, 1, 104, 0, 3, 8, 102, -1, 8, 10, 101, 1, 10, 10, 4, 10, 1008, 8, 0, 10, 4, 10, 102, 1, 8, 29, 3, 8, 1002, 8, -1, 10, 1001, 10, 1, 10, 4, 10, 1008, 8, 0, 10, 4, 10, 101, 0, 8, 51, 1, 1103, 2, 10, 1006, 0, 94, 1006, 0, 11, 1, 1106, 13, 10, 3, 8, 1002, 8, -1, 10, 101, 1, 10, 10, 4, 10, 1008, 8, 1, 10, 4, 10, 1001, 8, 0, 87, 3, 8, 102, -1, 8, 10, 101, 1, 10, 10, 4, 10, 1008, 8, 0, 10, 4, 10, 1001, 8, 0, 109, 2, 1105, 5, 10, 2, 103, 16, 10, 1, 1103, 12, 10, 2, 105, 2, 10, 3, 8, 102, -1, 8, 10, 1001, 10, 1, 10, 4, 10, 108, 1, 8, 10, 4, 10, 1001, 8, 0, 146, 1006, 0, 49, 2, 1, 12, 10, 2, 1006, 6, 10, 1, 1101, 4, 10, 3, 8, 1002, 8, -1, 10, 1001, 10, 1, 10, 4, 10, 108, 0, 8, 10, 4, 10, 1001, 8, 0, 183, 1, 6, 9, 10, 1006, 0, 32, 3, 8, 102, -1, 8, 10, 1001, 10, 1, 10, 4, 10, 1008, 8, 1, 10, 4, 10, 101, 0, 8, 213, 2, 1101, 9, 10, 3, 8, 1002, 8, -1, 10, 1001, 10, 1, 10, 4, 10, 1008, 8, 1, 10, 4, 10, 101, 0, 8, 239, 1006, 0, 47, 1006, 0, 4, 2, 6, 0, 10, 1006, 0, 58, 3, 8, 1002, 8, -1, 10, 1001, 10, 1, 10, 4, 10, 1008, 8, 0, 10, 4, 10, 102, 1, 8, 274, 2, 1005, 14, 10, 1006, 0, 17, 1, 104, 20, 10, 1006, 0, 28, 3, 8, 102, -1, 8, 10, 1001, 10, 1, 10, 4, 10, 108, 1, 8, 10, 4, 10, 1002, 8, 1, 309, 101, 1, 9, 9, 1007, 9, 928, 10, 1005, 10, 15, 99, 109, 652, 104, 0, 104, 1, 21101, 0, 937263411860, 1, 21102, 347, 1, 0, 1105, 1, 451, 21101, 932440724376, 0, 1, 21102, 1, 358, 0, 1105, 1, 451, 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, 21101, 0, 29015167015, 1, 21101, 0, 405, 0, 1106, 0, 451, 21102, 1, 3422723163, 1, 21101, 0, 416, 0, 1106, 0, 451, 3, 10, 104, 0, 104, 0, 3, 10, 104, 0, 104, 0, 21101, 0, 868389376360, 1, 21101, 0, 439, 0, 1105, 1, 451, 21102, 825544712960, 1, 1, 21102, 1, 450, 0, 1106, 0, 451, 99, 109, 2, 21201, -1, 0, 1, 21101, 0, 40, 2, 21102, 482, 1, 3, 21102, 1, 472, 0, 1106, 0, 515, 109, -2, 2106, 0, 0, 0, 1, 0, 0, 1, 109, 2, 3, 10, 204, -1, 1001, 477, 478, 493, 4, 0, 1001, 477, 1, 477, 108, 4, 477, 10, 1006, 10, 509, 1101, 0, 0, 477, 109, -2, 2106, 0, 0, 0, 109, 4, 2101, 0, -1, 514, 1207, -3, 0, 10, 1006, 10, 532, 21102, 1, 0, -3, 22101, 0, -3, 1, 22102, 1, -2, 2, 21102, 1, 1, 3, 21101, 551, 0, 0, 1106, 0, 556, 109, -4, 2105, 1, 0, 109, 5, 1207, -3, 1, 10, 1006, 10, 579, 2207, -4, -2, 10, 1006, 10, 579, 22102, 1, -4, -4, 1106, 0, 647, 21201, -4, 0, 1, 21201, -3, -1, 2, 21202, -2, 2, 3, 21102, 1, 598, 0, 1106, 0, 556, 22101, 0, 1, -4, 21101, 1, 0, -1, 2207, -4, -2, 10, 1006, 10, 617, 21102, 0, 1, -1, 22202, -2, -1, -2, 2107, 0, -3, 10, 1006, 10, 639, 21201, -1, 0, 1, 21102, 639, 1, 0, 105, 1, 514, 21202, -2, -1, -2, 22201, -4, -2, -4, 109, -5, 2105, 1, 0];
  4. /** All possible vectors for the robot to move */
  5. const Vectors = {
  6. NORTH: {
  7. x: 0,
  8. y: -1,
  9. },
  10. SOUTH: {
  11. x: 0,
  12. y: 1,
  13. },
  14. EAST: {
  15. x: 1,
  16. y: 0,
  17. },
  18. WEST: {
  19. x: -1,
  20. y: 0,
  21. },
  22. };
  23. /**
  24. * Data for tracking the robot's movement
  25. */
  26. const mapData = {
  27. /**
  28. * Tiles we've painted, value is the current color
  29. */
  30. grid: new Map(),
  31. /**
  32. * Unique number of tiles we've painted
  33. */
  34. uniquePaintings: 0,
  35. /**
  36. * The current coordinates of the robot
  37. */
  38. position: {
  39. x: 0,
  40. y: 0,
  41. },
  42. /**
  43. * The current direction the robot is facing
  44. */
  45. vector: Vectors.NORTH,
  46. /**
  47. * Paint the tile below the robot a given color
  48. *
  49. * @param {number} tileColor The new color for the tile below the robot
  50. * @returns {void}
  51. */
  52. PaintTile: function (tileColor) {
  53. const hashMapKey = `${this.position.x},${this.position.y}`;
  54. const uniqueTile = this.grid.get(hashMapKey) === undefined;
  55. this.grid.set(hashMapKey, tileColor);
  56. if (uniqueTile) {
  57. this.uniquePaintings++;
  58. }
  59. },
  60. /**
  61. * Set the heading for the robot
  62. *
  63. * @param {boolean} rotateLeft Should the robot rotate left? If not, rotate right
  64. * @returns {void}
  65. */
  66. SetHeading: function (rotateLeft) {
  67. switch (this.vector) {
  68. case Vectors.NORTH:
  69. // console.log(`DEBUG: Facing North, turning ${rotateLeft ? "West" : "East"}`);
  70. this.vector = rotateLeft ? Vectors.WEST : Vectors.EAST;
  71. break;
  72. case Vectors.SOUTH:
  73. // console.log(`DEBUG: Facing South, turning ${rotateLeft ? "East" : "West"}`);
  74. this.vector = rotateLeft ? Vectors.EAST : Vectors.WEST;
  75. break;
  76. case Vectors.EAST:
  77. // console.log(`DEBUG: Facing West, turning ${rotateLeft ? "North" : "South"}`);
  78. this.vector = rotateLeft ? Vectors.NORTH : Vectors.SOUTH;
  79. break;
  80. case Vectors.WEST:
  81. // console.log(`DEBUG: Facing East, turning ${rotateLeft ? "South" : "North"}`);
  82. this.vector = rotateLeft ? Vectors.SOUTH : Vectors.NORTH;
  83. break;
  84. default:
  85. throw new Error("Wuh?");
  86. }
  87. },
  88. /**
  89. * Move the robot along the current vector
  90. * @returns {void}
  91. */
  92. Move: function () {
  93. this.position.x += this.vector.x;
  94. this.position.y += this.vector.y;
  95. },
  96. };
  97. // Create and run the computer, Robby
  98. const robby = new Computer(input);
  99. robby.Run();
  100. let loopCount = 0;
  101. do {
  102. const currentColor = mapData.grid.get(`${mapData.position.x},${mapData.position.y}`) ?? 0;
  103. robby.Input(currentColor);
  104. const nextColor = robby.FetchOutputValue();
  105. const nextRotation = robby.FetchOutputValue();
  106. mapData.SetHeading(nextRotation == 0);
  107. mapData.PaintTile(nextColor);
  108. mapData.Move();
  109. if (++loopCount > 10_000_000) { console.error("Loop went on for too long."); process.exit(0); }
  110. } while (robby.running);
  111. console.log(`Total unique painted tiles: ${mapData.uniquePaintings}`);