Skip to content

Instantly share code, notes, and snippets.

@fu5ha
Last active June 16, 2017 08:56
Show Gist options
  • Save fu5ha/27c209456bb3aa8e2eed8bfdfa551a3a to your computer and use it in GitHub Desktop.
Save fu5ha/27c209456bb3aa8e2eed8bfdfa551a3a to your computer and use it in GitHub Desktop.
var fs = require("fs");
var inputData = JSON.parse(fs.readFileSync('data.json', 'utf-8'));
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
var level3Types = [
"Level 3 Military Vest",
"Level 3 Helmet",
"Level 3 Backpack"
];
var sniperTypes = [
"SKS",
"M24",
"AWM",
"Kar98",
"VSS"
];
function numberOfCratesWithTypes(data, types) {
return data.filter((package) => {
return types.reduce((hasItem, item) => hasItem || item in package, false);
}).length;
}
function totalNumberOfTypes(data, types) {
return data.reduce((totalItems, package) => {
return totalItems + types.reduce((itemsInPackage, item) => {
return itemsInPackage + (item in package ? package[item] : 0);
}, 0);
}, 0);
}
function chanceOfCrateWithTypes(data, types) {
return numberOfCratesWithTypes(data, types) / data.length;
}
function numberOfCratesWithTwoSameTypes(data, types) {
return data.filter((package) => {
return (types.reduce((itemsInPackage, item) => itemsInPackage + (item in package ? package[item] : 0), 0)) > 2;
}).length;
}
function numberOfCratesWithTwoDifferentTypes(data, types1, types2) {
return data.filter(package => {
var containsType1 = types1.reduce((hasItem, item) => hasItem || item in package, false);
var containsType2 = types2.reduce((hasItem, item) => hasItem || item in package, false);
return containsType1 && containsType2;
}).length;
}
function numberOfCratesWithTypes1NotTypes2(data, types1, types2) {
return data.filter(package => {
var containsType1 = types1.reduce((hasItem, item) => hasItem || item in package, false);
var containsType2 = types2.reduce((hasItem, item) => hasItem || item in package, false);
return containsType1 && !containsType2;
}).length;
}
function numberOfCratesWithoutTypes(data, types) {
return data.filter(package => {
return !(types.reduce((hasItem, item) => hasItem || item in package, false));
}).length;
}
function chanceOfCrateWithTypes1GivenTypes2(data, types1, types2) {
return numberOfCratesWithTwoDifferentTypes(data,types1,types2) / numberOfCratesWithTypes(data,types2);
}
function chanceOfCrateWithTypes1NotTypes2(data, types1, types2) {
return numberOfCratesWithTypes1NotTypes2(data,types1,types2) / numberOfCratesWithoutTypes(data,types2);
}
console.log("Number of crates with one or more level 3 gear pieces: " + round(numberOfCratesWithTypes(inputData, level3Types),2));
console.log("Number of crates with two or more level 3 gear pieces: " + round(numberOfCratesWithTwoSameTypes(inputData, level3Types),2));
console.log("");
console.log("Number of crates with snipers: " + round(numberOfCratesWithTypes(inputData, sniperTypes),2));
console.log("Total number of snipers: " + round(totalNumberOfTypes(inputData, sniperTypes),2));
console.log("Chance to find crate with sniper weapon: " + round(chanceOfCrateWithTypes(inputData, sniperTypes),2));
console.log("");
console.log("Number of crates with 15x scopes: " + round(numberOfCratesWithTypes(inputData, ["15X Scope"]),2));
console.log("Total number of 15x scopes: " + round(totalNumberOfTypes(inputData, ["15X Scope"]),2));
console.log("Chance to find 15x scope: " + round(chanceOfCrateWithTypes(inputData, ["15X Scope"]),2));
console.log("");
console.log("Chance to find 15x scope when there is a sniper in the crate: " + round(chanceOfCrateWithTypes1GivenTypes2(inputData, ["15X Scope"], sniperTypes),2));
console.log("Chance to find 15x scope when there is no sniper in the crate: " + round(chanceOfCrateWithTypes1NotTypes2(inputData, ["15X Scope"], sniperTypes), 2));
/* ================ OUTPUTS =================
Number of crates with one or more level 3 gear pieces: 56
Number of crates with two or more level 3 gear pieces: 0
Number of crates with snipers: 62
Total number of snipers: 62
Chance to find crate with sniper weapon: 0.61
Number of crates with 15x scopes: 6
Total number of 15x scopes: 6
Chance to find 15x scope: 0.06
Chance to find 15x scope when there is a sniper in the crate: 0.06
Chance to find 15x scope when there is no sniper in the crate: 0.05
================ OUTPUTS ================== */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment