Skip to content

Instantly share code, notes, and snippets.

@jezinka

jezinka/main.js Secret

Last active Jun 16, 2017
Embed
What would you like to do?
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