isundil 7 years ago
parent
commit
300010b2e7
1 changed files with 94 additions and 0 deletions
  1. 94 0
      d11/main.js

+ 94 - 0
d11/main.js

@@ -0,0 +1,94 @@
+
+const SERIAL = 8444,
+    MAP_SIZE =300;
+
+var resultCache = [];
+
+function getLevel(px, py) {
+    return resultCache[px *MAP_SIZE +py] || (resultCache[px *MAP_SIZE +py] = (Math.floor((((px +10) *py +SERIAL) *(px +10)) / 100) %10) -5);
+}
+
+function sumSquare(px, py, size) {
+    var r = 0;
+    for (var i =0; i < size; ++i)
+        for (var j =0; j < size; ++j)
+            r += getLevel(px +i, py +j);
+    return writeCache(px, py, size, r);
+}
+
+var cache = {};
+
+function getCache(px, py, size) {
+    if (!cache[size])
+        return;
+    if (!cache[size][px])
+        return;
+    return (cache[size][px][py])
+}
+
+function writeCache(px, py, size, val) {
+    if (size < (MAP_SIZE /2) +1) {
+        cache[size] = cache[size] || [];
+        cache[size][px] = cache[size][px] || [];
+        cache[size][px][py] = val;
+    }
+    return val;
+}
+
+function sumSquareWithCache(px, py, size, toto) {
+    var cached = getCache(px, py, size);
+    if (cached !== undefined)
+        return cached;
+    if (size >= 2 && Math.floor(size /2) == size /2)
+        return writeCache(px, py, size, sumSquareWithCache(px, py, size /2, 1) +
+            sumSquareWithCache(px +size /2, py, size /2, 1) +
+            sumSquareWithCache(px +size /2, py +size /2, size /2, 1) +
+            sumSquareWithCache(px, py +size /2, size /2, 1));
+    else if (size > 8) {
+        var tmpResult = sumSquareWithCache(px, py, size -1, 1);
+        for (var i =0; i <= size -1; ++i)
+            tmpResult += getLevel(px +i, py +size -1);
+        for (var i =0; i < size -1; ++i)
+            tmpResult += getLevel(px +size -1, py +i);
+        return writeCache(px, py, size, tmpResult);
+    }
+    return sumSquare(px, py, size);
+}
+
+function printResult(r) {
+    console.log(r.px +"," +r.py +", " +r.size);
+}
+
+function ex1(size) {
+    var max = null;
+
+    for (var i =1; i <= MAP_SIZE -3; ++i)
+        for (var j =1; j <= MAP_SIZE -3; ++j) {
+            var val = sumSquareWithCache(i, j, size);
+            if (!max || val > max.val)
+                max = {
+                    px: i,
+                    py: j,
+                    val: val,
+                    size: size
+                };
+        }
+    return max;
+}
+
+function ex2() {
+    var max = null;
+
+    for (var i =1; i < MAP_SIZE; ++i) {
+        var val = ex1(i);
+        if (!max || (val && val.val > max.val))
+            max = val;
+        process.stdout.write(i +"/" +MAP_SIZE +"\r");
+    }
+    process.stdout.write("\n");
+    printResult(max);
+}
+
+printResult(ex1(3));
+ex2();
+