|
|
|
|
@@ -1,16 +1,85 @@
|
|
|
|
|
const ASSIGNMENT = 'assignment';
|
|
|
|
|
const ANSWER = 'answer';
|
|
|
|
|
const ALGORITHM_CHECKBOX = 'algorithm';
|
|
|
|
|
const MARKER_DISTINCT_CHARACTERS = 4;
|
|
|
|
|
const MESSAGE_DISTINCT_CHARACTERS = 14;
|
|
|
|
|
const NEWLINE_CHARACTER = '\n';
|
|
|
|
|
const MAX_SIZE = 100000;
|
|
|
|
|
const UPDATE_SIZE = 30000000;
|
|
|
|
|
const TOTAL_SPACE = 70000000;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Main function
|
|
|
|
|
*/
|
|
|
|
|
window.onload = function() {
|
|
|
|
|
document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer);
|
|
|
|
|
document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", calculateAnswer);
|
|
|
|
|
document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TreeNode {
|
|
|
|
|
constructor(value, parent) {
|
|
|
|
|
console.debug(((!parent)?"Root ":"") + "node created: " + value);
|
|
|
|
|
|
|
|
|
|
this.parent = parent;
|
|
|
|
|
this.value = value;
|
|
|
|
|
this.size = 0;
|
|
|
|
|
this.descendants = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getDir = function(path) {
|
|
|
|
|
for(let descendant of this.descendants) {
|
|
|
|
|
if(descendant.value == path) {
|
|
|
|
|
return descendant;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contains = function(path) {
|
|
|
|
|
// Recursive case
|
|
|
|
|
let subPathIndex = path.indexOf('/');
|
|
|
|
|
let subPath = path.substring(0, subPathIndex-1);
|
|
|
|
|
let leftOverPath = path.substring(subPathIndex);
|
|
|
|
|
|
|
|
|
|
// Base case
|
|
|
|
|
if(subPath == '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(let descendant of this.descendants) {
|
|
|
|
|
if(descendant.value == subPath) {
|
|
|
|
|
return descendant.contains(subPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findClosestNodeToSize = function(minSize) {
|
|
|
|
|
let closest = this;
|
|
|
|
|
|
|
|
|
|
for(let descendant of this.descendants) {
|
|
|
|
|
let closestDescendant = descendant.findClosestNodeToSize(minSize);
|
|
|
|
|
|
|
|
|
|
if (closestDescendant.size > minSize && closestDescendant.size < closest.size) {
|
|
|
|
|
console.debug("New closest:" + closestDescendant.size);
|
|
|
|
|
closest = closestDescendant;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return closest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
increaseSize = function(size) {
|
|
|
|
|
// TODO: Wat doen we met duplicate files?
|
|
|
|
|
this.size += size;
|
|
|
|
|
if(this.parent) {
|
|
|
|
|
this.parent.increaseSize(size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getSum = function(maxSize) {
|
|
|
|
|
let result = (this.size < maxSize) ? this.size: 0;
|
|
|
|
|
|
|
|
|
|
for(let descendant of this.descendants) {
|
|
|
|
|
result += descendant.getSum(maxSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -19,9 +88,7 @@ window.onload = function() {
|
|
|
|
|
*/
|
|
|
|
|
function calculateAnswer(event) {
|
|
|
|
|
console.info("Calculating answer for input...");
|
|
|
|
|
let assignment = document.getElementById(ASSIGNMENT).value;
|
|
|
|
|
let distinctCharacters = (document.getElementById(ALGORITHM_CHECKBOX).checked)? MESSAGE_DISTINCT_CHARACTERS: MARKER_DISTINCT_CHARACTERS;
|
|
|
|
|
let answer = algorithm(assignment, distinctCharacters);
|
|
|
|
|
let answer = algorithm(event.target.value);
|
|
|
|
|
|
|
|
|
|
document.getElementById(ANSWER).innerText = answer;
|
|
|
|
|
}
|
|
|
|
|
@@ -31,28 +98,50 @@ function calculateAnswer(event) {
|
|
|
|
|
* @param assignment the input from the assignment.
|
|
|
|
|
* @return string the answer
|
|
|
|
|
*/
|
|
|
|
|
function algorithm(assignment, distinctCharacters) {
|
|
|
|
|
let signal = assignment.trim();
|
|
|
|
|
let marker = assignment.substring(0,distinctCharacters);
|
|
|
|
|
function algorithm(assignment) {
|
|
|
|
|
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
|
|
|
|
console.info("Linecount:" + lines.length);
|
|
|
|
|
|
|
|
|
|
for(let i=distinctCharacters; i<signal.length; i++) {
|
|
|
|
|
if(containsDistinctCharacters(marker)) {
|
|
|
|
|
console.debug("Found marker after:" + i + " characters");
|
|
|
|
|
return "Marker found after processing: " + i + " characters.";
|
|
|
|
|
let root = new TreeNode('/');
|
|
|
|
|
let currentdir = root;
|
|
|
|
|
|
|
|
|
|
for(let i=0; i<lines.length; i++) {
|
|
|
|
|
let terminalLine = lines[i].trim();
|
|
|
|
|
|
|
|
|
|
if(terminalLine.substring(0, 4) == "$ cd") {
|
|
|
|
|
let changeDirectory = terminalLine.substring(5).trim();
|
|
|
|
|
if(changeDirectory == "..") {
|
|
|
|
|
console.debug("One directory up from: " + currentdir.value);
|
|
|
|
|
currentdir = currentdir.parent;
|
|
|
|
|
console.debug("navigates to: " + currentdir.value);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
marker = marker.substring(1, distinctCharacters) + assignment[i];
|
|
|
|
|
if(changeDirectory == "/") {
|
|
|
|
|
currentdir = root;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!currentdir.contains(changeDirectory)) {
|
|
|
|
|
currentdir.descendants.push(new TreeNode(changeDirectory, currentdir));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentdir = currentdir.getDir(changeDirectory);
|
|
|
|
|
} else if(terminalLine.substring(0, 3) != 'dir' && !terminalLine.includes('$')) {
|
|
|
|
|
console.debug(terminalLine);
|
|
|
|
|
spaceIndex = terminalLine.indexOf(' ');
|
|
|
|
|
size = parseInt(terminalLine.substring(0, spaceIndex));
|
|
|
|
|
currentdir.increaseSize(size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "No marker found.";
|
|
|
|
|
}
|
|
|
|
|
let freeDiskSpace = TOTAL_SPACE - root.size;
|
|
|
|
|
console.debug("Free disk space: " + freeDiskSpace);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if string only contains distinct characters.
|
|
|
|
|
* @param String string the string to check
|
|
|
|
|
* @return true, if string contains only distinct characters
|
|
|
|
|
*/
|
|
|
|
|
function containsDistinctCharacters(string) {
|
|
|
|
|
console.debug("Check marker: " + string);
|
|
|
|
|
return new Set(string).size == string.length;
|
|
|
|
|
console.debug("Root size:" + root.size);
|
|
|
|
|
console.debug("Value to find: " + (UPDATE_SIZE - freeDiskSpace));
|
|
|
|
|
let closestNode = root.findClosestNodeToSize(UPDATE_SIZE - freeDiskSpace);
|
|
|
|
|
console.debug("Closest value: " + closestNode.size);
|
|
|
|
|
|
|
|
|
|
return "Sum below maxSize " + MAX_SIZE + ": " + root.getSum(MAX_SIZE);
|
|
|
|
|
}
|