Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 7a3dd6bcde |
@@ -1,6 +1,5 @@
|
|||||||
# Advent of Code 2022 - Assignment7 javascript
|
# Advent of Code 2022 - Assignment6_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.
|
This repository contains answers to the assignments of Advent of Coding 2022
|
||||||
|
|
||||||
## References
|
## References
|
||||||
- https://adrianmejia.com/data-structures-for-beginners-trees-binary-search-tree-tutorial/
|
- https://stackoverflow.com/questions/28207610/checking-if-the-characters-in-a-string-are-all-unique
|
||||||
- https://stackoverflow.com/questions/8376525/get-value-of-a-string-after-last-slash-in-javascript
|
|
||||||
@@ -8,7 +8,9 @@
|
|||||||
<body>
|
<body>
|
||||||
<p>Assignment:</p>
|
<p>Assignment:</p>
|
||||||
<textarea rows="15" cols="50" id="assignment"></textarea>
|
<textarea rows="15" cols="50" id="assignment"></textarea>
|
||||||
|
<input type="checkbox" id="algorithm" />
|
||||||
|
<label for="algorithm">Search for message marker?</label>
|
||||||
|
|
||||||
<p>Answer:</p>
|
<p>Answer:</p>
|
||||||
<div id="answer">Provide input first</div>
|
<div id="answer">Provide input first</div>
|
||||||
<script src="script.js"></script>
|
<script src="script.js"></script>
|
||||||
|
|||||||
141
script.js
141
script.js
@@ -1,85 +1,16 @@
|
|||||||
const ASSIGNMENT = 'assignment';
|
const ASSIGNMENT = 'assignment';
|
||||||
const ANSWER = 'answer';
|
const ANSWER = 'answer';
|
||||||
|
const ALGORITHM_CHECKBOX = 'algorithm';
|
||||||
|
const MARKER_DISTINCT_CHARACTERS = 4;
|
||||||
|
const MESSAGE_DISTINCT_CHARACTERS = 14;
|
||||||
const NEWLINE_CHARACTER = '\n';
|
const NEWLINE_CHARACTER = '\n';
|
||||||
const MAX_SIZE = 100000;
|
|
||||||
const UPDATE_SIZE = 30000000;
|
|
||||||
const TOTAL_SPACE = 70000000;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function
|
* Main function
|
||||||
*/
|
*/
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer);
|
document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer);
|
||||||
}
|
document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,7 +19,9 @@ class TreeNode {
|
|||||||
*/
|
*/
|
||||||
function calculateAnswer(event) {
|
function calculateAnswer(event) {
|
||||||
console.info("Calculating answer for input...");
|
console.info("Calculating answer for input...");
|
||||||
let answer = algorithm(event.target.value);
|
let assignment = document.getElementById(ASSIGNMENT).value;
|
||||||
|
let distinctCharacters = (document.getElementById(ALGORITHM_CHECKBOX).checked)? MESSAGE_DISTINCT_CHARACTERS: MARKER_DISTINCT_CHARACTERS;
|
||||||
|
let answer = algorithm(assignment, distinctCharacters);
|
||||||
|
|
||||||
document.getElementById(ANSWER).innerText = answer;
|
document.getElementById(ANSWER).innerText = answer;
|
||||||
}
|
}
|
||||||
@@ -98,50 +31,28 @@ function calculateAnswer(event) {
|
|||||||
* @param assignment the input from the assignment.
|
* @param assignment the input from the assignment.
|
||||||
* @return string the answer
|
* @return string the answer
|
||||||
*/
|
*/
|
||||||
function algorithm(assignment) {
|
function algorithm(assignment, distinctCharacters) {
|
||||||
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
let signal = assignment.trim();
|
||||||
console.info("Linecount:" + lines.length);
|
let marker = assignment.substring(0,distinctCharacters);
|
||||||
|
|
||||||
let root = new TreeNode('/');
|
for(let i=distinctCharacters; i<signal.length; i++) {
|
||||||
let currentdir = root;
|
if(containsDistinctCharacters(marker)) {
|
||||||
|
console.debug("Found marker after:" + i + " characters");
|
||||||
for(let i=0; i<lines.length; i++) {
|
return "Marker found after processing: " + i + " characters.";
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(changeDirectory == "/") {
|
marker = marker.substring(1, distinctCharacters) + assignment[i];
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let freeDiskSpace = TOTAL_SPACE - root.size;
|
return "No marker found.";
|
||||||
console.debug("Free disk space: " + freeDiskSpace);
|
}
|
||||||
|
|
||||||
console.debug("Root size:" + root.size);
|
/**
|
||||||
console.debug("Value to find: " + (UPDATE_SIZE - freeDiskSpace));
|
* Check if string only contains distinct characters.
|
||||||
let closestNode = root.findClosestNodeToSize(UPDATE_SIZE - freeDiskSpace);
|
* @param String string the string to check
|
||||||
console.debug("Closest value: " + closestNode.size);
|
* @return true, if string contains only distinct characters
|
||||||
|
*/
|
||||||
return "Sum below maxSize " + MAX_SIZE + ": " + root.getSum(MAX_SIZE);
|
function containsDistinctCharacters(string) {
|
||||||
|
console.debug("Check marker: " + string);
|
||||||
|
return new Set(string).size == string.length;
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,10 @@ body {
|
|||||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
textarea {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
#answer {
|
#answer {
|
||||||
color: purple
|
color: purple
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user