diff --git a/README.md b/README.md index da57339..ea1f596 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ -This repository contains answers to the assignments of Advent of Coding 2022 +# Advent of Code 2022 - Assignment7 javascript +Assignment 7 involves parsing linux directory commands/results into a tree. The solution defines a root TreeNode first and sets it as currentdirectory. Whenever a cd command is found a new node is created if it does not exist and set to currentdir. Whenever a cd .. is found currentdirectory is set to parent. Whenever a file is found size is recursively increased for current node and parent nodes. + +## References +- https://adrianmejia.com/data-structures-for-beginners-trees-binary-search-tree-tutorial/ +- https://stackoverflow.com/questions/8376525/get-value-of-a-string-after-last-slash-in-javascript diff --git a/script.js b/script.js index a6e2848..ac54366 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,9 @@ const ASSIGNMENT = 'assignment'; const ANSWER = 'answer'; const NEWLINE_CHARACTER = '\n'; +const MAX_SIZE = 100000; +const UPDATE_SIZE = 30000000; +const TOTAL_SPACE = 70000000; /** * Main function @@ -9,6 +12,76 @@ 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 @@ -26,8 +99,49 @@ function calculateAnswer(event) { * @return string the answer */ function algorithm(assignment) { - let lines = assignment.trim().split(NEWLINE_CHARACTER); - console.info("Linecount:" + lines.length); + let lines = assignment.trim().split(NEWLINE_CHARACTER); + console.info("Linecount:" + lines.length); - // TODO: implement assignment + let root = new TreeNode('/'); + let currentdir = root; + + for(let i=0; i