const ASSIGNMENT = 'assignment'; const ANSWER = 'answer'; 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); } 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; } } /** * Listener function for input in assignment field. * @param event the onInput event */ function calculateAnswer(event) { console.info("Calculating answer for input..."); let answer = algorithm(event.target.value); document.getElementById(ANSWER).innerText = answer; } /** * Calculate the answer to assignment. * @param assignment the input from the assignment. * @return string the answer */ function algorithm(assignment) { let lines = assignment.trim().split(NEWLINE_CHARACTER); console.info("Linecount:" + lines.length); let root = new TreeNode('/'); let currentdir = root; for(let i=0; i