<commit_before>var totalKmWalked = null;
var distanceTravelledToGetNewIncubator = null; 

var chance10kmDistribMax = null;
var chance2kmDistribMax = null;

var ALL_EGG_TYPES = [2, 5, 10];

var combinations = findCombinations(ALL_EGG_TYPES);

var permutations = [];
var c = 0;
for (c = 0; c < combinations.length; c++) {
	permutations = permutations.concat(findPermutations(combinations[c]));
}

var testCases = [];
var numPermutations = permutations.length;
var p = 0;
for (p = 0; p < numPermutations; p++) {
	var q = 0;
	for (q = 0; q < numPermutations; q++) {
		var permutationBlue = permutations[p];
		var permutationOrange = permutations[q];
		var merged = permutationBlue.concat(permutationOrange);
		if (accountsForAllEggTypes(merged)) {
			testCases[testCases.length] = { blueStrategy : permutationBlue, orangeStrategy : permutationOrange };
		}
	}
}
var TOTAL_EGG_SLOTS = 9;
var bestFor10kmHatching = [];
var eggCache = [];
var tenKmHatches = 0;

function accountsForAllEggTypes(merged) {
	return contains(merged, 10) && contains(merged, 5) && contains(merged, 2);
}

function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return true;
       }
    }
    return false;
}

function run() {
	try {
		totalKmWalked = document.getElementById("totalKmWalked").value;
		distanceTravelledToGetNewIncubator = document.getElementById("kmPerNewIncubator").value; 
		var chance10km = document.getElementById("chance10km").value / 100;
		var chance2km = document.getElementById("chance2km").value / 100;
		var chance5km = 1 - chance10km - chance2km;

		document.getElementById("calculatedChance5km").innerHTML = "Chance of 5 km egg (%): " + (chance5km * 100);

		chance10kmDistribMax = chance10km;
		chance2kmDistribMax = chance10km + chance2km;

		var results = document.getElementById("results");
		results.innerHTML = "";

		// use same initial eggs for every strategy
		eggCache = [];
		var initEggs = [];
		var i = 0;
		for (i = 0; i < TOTAL_EGG_SLOTS; i++) {
			initEggs[initEggs.length] = generateRandomEgg();
		}

		bestFor10kmHatching = [];
		var bestTotalHatches = 0;
		var totalTestCases = testCases.length;
		var c = 0;
		for (c = 0; c < totalTestCases; c++) {
			var testCase = testCases[c];
			testCase.eggSlots = [];
			testCase.blueIncubators = [];
			testCase.orangeIncubator = { remUses : Infinity, isOccupied : false };
			testCase.hatchedEggs = [];

			var id = getTestCaseId(testCase);
			tenKmHatches = 0;
			createTestCaseHeaderDesc(results, id, testCase.blueStrategy, testCase.orangeStrategy);

			var table = document.createElement("table");
			table.id = "eggSlots" + (c+1);
			results.appendChild(table);

			var tableInner = "<tr><th>Distance walked (km)</th>";

			var i = 0;
			for (i = 0; i < TOTAL_EGG_SLOTS; i++) {
				var egg = { type : initEggs[i].type, remIncubation : initEggs[i].remIncubation };
				testCase.eggSlots[i] = {egg : egg, incubator : null};

				// create headings in eggSlots tables
				tableInner += "<th>Egg Slot " + (i+1) + "</th>";
			}
			tableInner += "<th>Blue Incubators</th><th>10km hatches</th><th>Total Hatches</th></tr>";
			tableInner += walk(testCase);
			table.innerHTML = tableInner;

			var hatched10kmCount = 0;
			var hatched5kmCount = 0;
			var hatched2kmCount = 0;
			var h = 0;
			for (h = 0; h < testCase.hatchedEggs.length; h++) {
				var pokemon = testCase.hatchedEggs[h];
				if (pokemon.type == 10) {
					hatched10kmCount++;
				} else if (pokemon.type == 5) {
					hatched5kmCount++;
				} else if (pokemon.type == 2) {
					hatched2kmCount++;
				} 
			}

			var totalHatched = testCase.hatchedEggs.length;
			createTestCaseFooter(c, results, hatched10kmCount, hatched5kmCount, hatched2kmCount, totalHatched, testCase.blueIncubators);

			var inserted = false;
			var count = bestFor10kmHatching.length;
			var m = 0;
// try binary search instead
			while (!inserted && m < count) {
				if (isBetter(hatched10kmCount, totalHatched, bestFor10kmHatching[m].hatched10kmCount, bestFor10kmHatching[m].totalHatched)) {
					bestFor10kmHatching.splice(m, 0, { hatched10kmCount : hatched10kmCount, totalHatched : totalHatched, testCase : testCase, id : id });
					inserted = true;
				}
				m++;
			}
			if (!inserted) {
				bestFor10kmHatching[count] = { hatched10kmCount : hatched10kmCount, totalHatched : totalHatched, testCase : testCase, id : id };
			}
			if (totalHatched > bestTotalHatches) {
				bestTotalHatches = totalHatched;
			}
		}
		printBestResultsDesc(bestTotalHatches);

	} catch (err) {
		document.getElementById("errors").innerHTML = err.message; 
	}
}

function walk(testCase) {
	var rows = "";
	var km = 0;
	var eggIndex = 0;
	var blueStrategyLength = testCase.blueStrategy.length;
	var orangeStrategyLength = testCase.blueStrategy.length;
	for (km = 0; km < totalKmWalked; km++) {
		var newInc = 0;
		if (km % distanceTravelledToGetNewIncubator == 0) {
			newInc = 1;
			testCase.blueIncubators[testCase.blueIncubators.length] = { remUses : 3, isOccupied : false };
		}
		rows += printDetails(testCase.eggSlots, km, testCase.blueIncubators.length, testCase.hatchedEggs.length);
		useBlueIncubatorStrategy(testCase.blueIncubators, testCase.eggSlots, testCase.blueStrategy, blueStrategyLength);
		useStrategy(testCase.orangeIncubator, testCase.eggSlots, testCase.orangeStrategy, orangeStrategyLength);
		var s = 0;
		for (s = 0; s < TOTAL_EGG_SLOTS; s++) {
			if (testCase.eggSlots[s] == null) {
				testCase.eggSlots[s] = { egg : getNextEgg(eggIndex), incubator : null };
				eggIndex++;
			}
		}
		incrementIncubationTime(testCase.eggSlots);
		findHatchedEggs(testCase.hatchedEggs, testCase.eggSlots);<commit_msg>fix bug<commit_after>var totalKmWalked = null;
var distanceTravelledToGetNewIncubator = null; 

var chance10kmDistribMax = null;
var chance2kmDistribMax = null;

var ALL_EGG_TYPES = [2, 5, 10];

var combinations = findCombinations(ALL_EGG_TYPES);

var permutations = [];
var c = 0;
for (c = 0; c < combinations.length; c++) {
	permutations = permutations.concat(findPermutations(combinations[c]));
}

var testCases = [];
var numPermutations = permutations.length;
var p = 0;
for (p = 0; p < numPermutations; p++) {
	var q = 0;
	for (q = 0; q < numPermutations; q++) {
		var permutationBlue = permutations[p];
		var permutationOrange = permutations[q];
		var merged = permutationBlue.concat(permutationOrange);
		if (accountsForAllEggTypes(merged)) {
			testCases[testCases.length] = { blueStrategy : permutationBlue, orangeStrategy : permutationOrange };
		}
	}
}
var TOTAL_EGG_SLOTS = 9;
var bestFor10kmHatching = [];
var eggCache = [];
var tenKmHatches = 0;

function accountsForAllEggTypes(merged) {
	return contains(merged, 10) && contains(merged, 5) && contains(merged, 2);
}

function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return true;
       }
    }
    return false;
}

function run() {
	try {
		totalKmWalked = document.getElementById("totalKmWalked").value;
		distanceTravelledToGetNewIncubator = document.getElementById("kmPerNewIncubator").value; 
		var chance10km = document.getElementById("chance10km").value / 100;
		var chance2km = document.getElementById("chance2km").value / 100;
		var chance5km = 1 - chance10km - chance2km;

		document.getElementById("calculatedChance5km").innerHTML = "Chance of 5 km egg (%): " + (chance5km * 100);

		chance10kmDistribMax = chance10km;
		chance2kmDistribMax = chance10km + chance2km;

		var results = document.getElementById("results");
		results.innerHTML = "";

		// use same initial eggs for every strategy
		eggCache = [];
		var initEggs = [];
		var i = 0;
		for (i = 0; i < TOTAL_EGG_SLOTS; i++) {
			initEggs[initEggs.length] = generateRandomEgg();
		}

		bestFor10kmHatching = [];
		var bestTotalHatches = 0;
		var totalTestCases = testCases.length;
		var c = 0;
		for (c = 0; c < totalTestCases; c++) {
			var testCase = testCases[c];
			testCase.eggSlots = [];
			testCase.blueIncubators = [];
			testCase.orangeIncubator = { remUses : Infinity, isOccupied : false };
			testCase.hatchedEggs = [];

			var id = getTestCaseId(testCase);
			tenKmHatches = 0;
			createTestCaseHeaderDesc(results, id, testCase.blueStrategy, testCase.orangeStrategy);

			var table = document.createElement("table");
			table.id = "eggSlots" + (c+1);
			results.appendChild(table);

			var tableInner = "<tr><th>Distance walked (km)</th>";

			var i = 0;
			for (i = 0; i < TOTAL_EGG_SLOTS; i++) {
				var egg = { type : initEggs[i].type, remIncubation : initEggs[i].remIncubation };
				testCase.eggSlots[i] = {egg : egg, incubator : null};

				// create headings in eggSlots tables
				tableInner += "<th>Egg Slot " + (i+1) + "</th>";
			}
			tableInner += "<th>Blue Incubators</th><th>10km hatches</th><th>Total Hatches</th></tr>";
			tableInner += walk(testCase);
			table.innerHTML = tableInner;

			var hatched10kmCount = 0;
			var hatched5kmCount = 0;
			var hatched2kmCount = 0;
			var h = 0;
			for (h = 0; h < testCase.hatchedEggs.length; h++) {
				var pokemon = testCase.hatchedEggs[h];
				if (pokemon.type == 10) {
					hatched10kmCount++;
				} else if (pokemon.type == 5) {
					hatched5kmCount++;
				} else if (pokemon.type == 2) {
					hatched2kmCount++;
				} 
			}

			var totalHatched = testCase.hatchedEggs.length;
			createTestCaseFooter(c, results, hatched10kmCount, hatched5kmCount, hatched2kmCount, totalHatched, testCase.blueIncubators);

			var inserted = false;
			var count = bestFor10kmHatching.length;
			var m = 0;
// try binary search instead
			while (!inserted && m < count) {
				if (isBetter(hatched10kmCount, totalHatched, bestFor10kmHatching[m].hatched10kmCount, bestFor10kmHatching[m].totalHatched)) {
					bestFor10kmHatching.splice(m, 0, { hatched10kmCount : hatched10kmCount, totalHatched : totalHatched, testCase : testCase, id : id });
					inserted = true;
				}
				m++;
			}
			if (!inserted) {
				bestFor10kmHatching[count] = { hatched10kmCount : hatched10kmCount, totalHatched : totalHatched, testCase : testCase, id : id };
			}
			if (totalHatched > bestTotalHatches) {
				bestTotalHatches = totalHatched;
			}
		}
		printBestResultsDesc(bestTotalHatches);

	} catch (err) {
		document.getElementById("errors").innerHTML = err.message; 
	}
}

function walk(testCase) {
	var rows = "";
	var km = 0;
	var eggIndex = 0;
	var blueStrategyLength = testCase.blueStrategy.length;
	var orangeStrategyLength = testCase.orangeStrategy.length;
	for (km = 0; km < totalKmWalked; km++) {
		var newInc = 0;
		if (km % distanceTravelledToGetNewIncubator == 0) {
			newInc = 1;
			testCase.blueIncubators[testCase.blueIncubators.length] = { remUses : 3, isOccupied : false };
		}
		rows += printDetails(testCase.eggSlots, km, testCase.blueIncubators.length, testCase.hatchedEggs.length);
		useBlueIncubatorStrategy(testCase.blueIncubators, testCase.eggSlots, testCase.blueStrategy, blueStrategyLength);
		useStrategy(testCase.orangeIncubator, testCase.eggSlots, testCase.orangeStrategy, orangeStrategyLength);
		var s = 0;
		for (s = 0; s < TOTAL_EGG_SLOTS; s++) {
			if (testCase.eggSlots[s] == null) {
				testCase.eggSlots[s] = { egg : getNextEgg(eggIndex), incubator : null };
				eggIndex++;
			}
		}
		incrementIncubationTime(testCase.eggSlots);
		findHatchedEggs(testCase.hatchedEggs, testCase.eggSlots);
{'fraction_non_alphanumeric': 0.13081138412586177, 'fraction_numerical': 0.02121265688527488, 'mean_word_length': 4.092259225922592, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1946222', 'n_tokens_mistral': 4703, 'n_tokens_neox': 3957, 'n_words': 844}