isundil 7 жил өмнө
parent
commit
91ec629fb6
1 өөрчлөгдсөн 82 нэмэгдсэн , 0 устгасан
  1. 82 0
      d9/main.js

+ 82 - 0
d9/main.js

@@ -0,0 +1,82 @@
+
+const NB_PLAYERS= 418,
+    LAST_MARBLE= 70769 * 100;
+
+function Node(val, next) {
+    this.prev = next ? next.prev : this;
+    this.next = next || this;
+
+    this.prev.next = this;
+    this.next.prev = this;
+
+    this.val = val;
+}
+
+Node.prototype.print = function() {
+    var i = this,
+        arr = [];
+    do {
+        arr.push(i.val);
+        i = i.next;
+    } while (i !== this);
+    console.log(arr);
+}
+
+Node.prototype.removeNext = function() {
+    var val = this.next;
+    this.next = this.next.next;
+    this.next.prev = this;
+    return val;
+}
+
+Node.prototype.removePrev = function() {
+    var val = this.prev;
+    this.prev = this.prev.prev;
+    this.prev.next = this;
+    return val;
+}
+
+function Board() {
+    this.scores = [];
+    this.currentPos = new Node(0);
+    this.turn = 0;
+
+    for (var i =0; i < NB_PLAYERS; ++i)
+        this.scores.push(0);
+}
+
+Board.prototype.nextTurn = function() {
+    var currentPlayer = this.turn %NB_PLAYERS,
+        currentMarple = ++this.turn;
+
+    if (currentMarple % 23) {
+        this.currentPos = new Node(currentMarple, this.currentPos.next.next);
+    } else {
+        this.currentPos = this.currentPos.prev.prev.prev.prev.prev.prev;
+        this.scores[currentPlayer] += this.currentPos.removePrev().val;
+        this.scores[currentPlayer] += currentMarple;
+    }
+}
+
+Board.prototype.find = function(val) {
+    var i = this.currentPos;
+    do {
+        if (i.val === val)
+            return i;
+        i = i.next;
+    } while (i !== this);
+}
+
+Board.prototype.maxScore = function() {
+    return Array.from(this.scores).sort()[this.scores.length -1];
+}
+
+function run() {
+    var board = new Board();
+    while (board.turn < LAST_MARBLE)
+        board.nextTurn();
+    console.log(board.maxScore());
+}
+
+run();
+