main.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const fs = require('fs');
  2. const readline = require('readline');
  3. const SCREEN_WIDTH = 40;
  4. //const RELIEF = BigInt(3); // part 1
  5. const RELIEF = BigInt(1); // part 2
  6. function Monkey(monkeyPool, id) {
  7. this.pool = monkeyPool;
  8. this.id = id;
  9. this.items = [];
  10. this.operation = i => i;
  11. this.testValue = 0;
  12. this.ifTrue = null;
  13. this.ifFalse = null;
  14. this.inspect = 0;
  15. }
  16. Monkey.prototype.setOperation = function(line) {
  17. this.operator = line.substring(0, 1);
  18. let other = line.substring(2);
  19. this.operation = i => {
  20. let _other = other === 'old' ? i : BigInt(parseInt(other));
  21. switch (this.operator) {
  22. case '+':
  23. i = i + _other; break;
  24. case '*':
  25. i = i * _other; break;
  26. }
  27. return RELIEF == 1 ? (i % this.pool.maxDiv): Math.floor(Number(i / RELIEF));
  28. };
  29. }
  30. Monkey.prototype.run = function() {
  31. this.inspect += this.items.length;
  32. while (this.items.length) {
  33. let i = this.items.shift();
  34. i = this.operation(i);
  35. this.pool.monkeys[(i % this.testValue == 0) ? this.ifTrue : this.ifFalse].items.push(i);
  36. }
  37. }
  38. function MonkeyPool() {
  39. this.monkeys = [];
  40. this.maxDiv = 0;
  41. }
  42. MonkeyPool.prototype.push = function(i) {
  43. let m = new Monkey(this, i);
  44. this.monkeys[i] = m;
  45. return i;
  46. }
  47. MonkeyPool.prototype.round = function() {
  48. for (let i of this.monkeys)
  49. i.run();
  50. }
  51. MonkeyPool.prototype.debug = function() {
  52. for (let i of this.monkeys) console.log(i.id, i.inspect, i.items);
  53. }
  54. async function main() {
  55. let monkeys = new MonkeyPool();
  56. let currentMonkey = null;
  57. for await (let line of readline.createInterface({ input: process.stdin })) {
  58. line = line.trim();
  59. if (!line || !line.length)
  60. currentMonkey = null;
  61. else if (line.startsWith('Monkey '))
  62. currentMonkey = monkeys.push(parseInt(line.substr(7)));
  63. else if (line.startsWith('Starting items: '))
  64. monkeys.monkeys[currentMonkey].items = line.substring('Starting items: '.length).split(',').map(i => BigInt(parseInt(i.trim())));
  65. else if (line.startsWith('Operation: new = old '))
  66. monkeys.monkeys[currentMonkey].setOperation(line.substring('Operation: new = old '.length));
  67. else if (line.startsWith('Test: divisible by '))
  68. monkeys.monkeys[currentMonkey].testValue = BigInt(parseInt(line.substring('Test: divisible by'.length)));
  69. else if (line.startsWith('If true: throw to monkey '))
  70. monkeys.monkeys[currentMonkey].ifTrue = parseInt(line.substring('If true: throw to monkey '.length));
  71. else if (line.startsWith('If false: throw to monkey '))
  72. monkeys.monkeys[currentMonkey].ifFalse = parseInt(line.substring('If false: throw to monkey '.length));
  73. }
  74. monkeys.maxDiv = monkeys.monkeys.map(i => i.testValue).reduce((a, b) => a ? (a * b) : b);
  75. for (let i =0; i < 10000; ++i)
  76. monkeys.round();
  77. console.log(monkeys.monkeys.map(i => i.inspect).sort((a, b) => b -a).slice(0, 2).reduce((a, b) => a ? a*b : b));
  78. console.log(monkeys.monkeys.map(i => i.inspect));
  79. };
  80. (main());