main.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const NB_PLAYERS= 418,
  2. LAST_MARBLE= 70769 * 100;
  3. function Node(val, next) {
  4. this.prev = next ? next.prev : this;
  5. this.next = next || this;
  6. this.prev.next = this;
  7. this.next.prev = this;
  8. this.val = val;
  9. }
  10. Node.prototype.print = function() {
  11. var i = this,
  12. arr = [];
  13. do {
  14. arr.push(i.val);
  15. i = i.next;
  16. } while (i !== this);
  17. console.log(arr);
  18. }
  19. Node.prototype.removeNext = function() {
  20. var val = this.next;
  21. this.next = this.next.next;
  22. this.next.prev = this;
  23. return val;
  24. }
  25. Node.prototype.removePrev = function() {
  26. var val = this.prev;
  27. this.prev = this.prev.prev;
  28. this.prev.next = this;
  29. return val;
  30. }
  31. function Board() {
  32. this.scores = [];
  33. this.currentPos = new Node(0);
  34. this.turn = 0;
  35. for (var i =0; i < NB_PLAYERS; ++i)
  36. this.scores.push(0);
  37. }
  38. Board.prototype.nextTurn = function() {
  39. var currentPlayer = this.turn %NB_PLAYERS,
  40. currentMarple = ++this.turn;
  41. if (currentMarple % 23) {
  42. this.currentPos = new Node(currentMarple, this.currentPos.next.next);
  43. } else {
  44. this.currentPos = this.currentPos.prev.prev.prev.prev.prev.prev;
  45. this.scores[currentPlayer] += this.currentPos.removePrev().val;
  46. this.scores[currentPlayer] += currentMarple;
  47. }
  48. }
  49. Board.prototype.find = function(val) {
  50. var i = this.currentPos;
  51. do {
  52. if (i.val === val)
  53. return i;
  54. i = i.next;
  55. } while (i !== this);
  56. }
  57. Board.prototype.maxScore = function() {
  58. return Array.from(this.scores).sort()[this.scores.length -1];
  59. }
  60. function run() {
  61. var board = new Board();
  62. while (board.turn < LAST_MARBLE)
  63. board.nextTurn();
  64. console.log(board.maxScore());
  65. }
  66. run();