Last active
August 29, 2015 14:03
-
-
Save a1ip/c8188f800a66d328e09c to your computer and use it in GitHub Desktop.
JavaScript solution for http://codecombat.com/play/level/danger-minefield level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var coin, firstMineIndex, lastMineIndex, pr; | |
| this.detonate = function(i) { | |
| return this.say("Detonate " + i, [i]); | |
| }; | |
| firstMineIndex = 2; | |
| lastMineIndex = 120; | |
| function eratosthenes(limit) { | |
| var primes = []; | |
| if (limit >= 2) { | |
| var sqrtlmt = Math.sqrt(limit) - 2; | |
| var nums = new Array(); // start with an empty Array... | |
| for (var i = 2; i <= limit; i++) // and | |
| nums.push(i); // only initialize the Array once... | |
| for (var i = 0; i <= sqrtlmt; i++) { | |
| var p = nums[i] | |
| if (p) | |
| for (var j = p * p - 2; j < nums.length; j += p) | |
| nums[j] = 0; | |
| } | |
| for (var i = 0; i < nums.length; i++) { | |
| var p = nums[i]; | |
| if (p) | |
| primes.push(p); | |
| } | |
| } | |
| return primes; | |
| } | |
| pr = eratosthenes(lastMineIndex); | |
| __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | |
| for (i = firstMineIndex; i <= lastMineIndex ; i++) { | |
| if (__indexOf.call(pr, i) < 0) { | |
| this.detonate(i); | |
| } | |
| } | |
| this.say("Woohoo, safe! Lootin' time!"); | |
| coins = this.getItems(); | |
| for (var i = 0; i < coins.length; i++) { | |
| this.move(coins[i].pos); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment