-
-
Save jezinka/8e345f9d3269e1921c900461573d2687 to your computer and use it in GitHub Desktop.
This file contains 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 sumId = 0; | |
for (var i = 0; i < instructions.length; i++) { | |
var instruction = instructions[i]; | |
var encrypted = instruction.match(/^[a-z]+/)[0]; | |
var id = parseInt(instruction.match(/\d+/)[0]); | |
var checksum = instruction.match(/\[([a-z]{5})\]/)[1]; | |
Array.prototype.byCount = function () { | |
var array_length = this.length, dict = {}; | |
for (var i = 0; i < array_length; i++) { | |
var letter = this[i]; | |
if (!letter) { | |
continue | |
} | |
if (dict[letter] === undefined) { | |
dict[letter] = 1 | |
} else { | |
++dict[letter] | |
} | |
} | |
var keys = Object.keys(dict); | |
return keys.sort(function (a, b) { | |
if (dict[b] !== dict[a]) { | |
return dict[b] - dict[a]; | |
} | |
return a.charCodeAt(0) - b.charCodeAt(0); | |
}); | |
}; | |
var letters_table = encrypted.split(""); | |
var sorted_letters_table = letters_table.byCount(); | |
var counted_checksum = sorted_letters_table.slice(0, 5).join(''); | |
if (checksum === counted_checksum) { | |
sumId += id | |
} | |
} | |
return sumId; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment