Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 0af89a72ba |
15
README.md
15
README.md
@@ -1,13 +1,6 @@
|
|||||||
# Advent of Code 2022 - Assignment5 javascript
|
# Advent of Code 2022 - Assignment7 javascript
|
||||||
This repository contains answers to the assignments of Advent of Coding 2022
|
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
|
## References
|
||||||
- https://www.w3schools.com/jsref/jsref_includes.asp
|
- https://adrianmejia.com/data-structures-for-beginners-trees-binary-search-tree-tutorial/
|
||||||
- https://stackoverflow.com/questions/10261986/how-to-detect-string-which-contains-only-spaces
|
- https://stackoverflow.com/questions/8376525/get-value-of-a-string-after-last-slash-in-javascript
|
||||||
- https://www.w3schools.com/jsref/jsref_indexof.asp
|
|
||||||
- https://stackoverflow.com/questions/8073673/how-can-i-add-new-array-elements-at-the-beginning-of-an-array-in-javascript
|
|
||||||
- https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript
|
|
||||||
- https://www.w3schools.com/jsref/jsref_ceil.asp
|
|
||||||
- https://stackoverflow.com/questions/30561056/console-log-a-multi-dimensional-array
|
|
||||||
- https://teamtreehouse.com/community/removing-more-than-1-element-using-pop-and-shift-method
|
|
||||||
- https://stackoverflow.com/questions/14723848/push-multiple-elements-to-array
|
|
||||||
|
|||||||
@@ -8,8 +8,6 @@
|
|||||||
<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">All containers at once?</label>
|
|
||||||
|
|
||||||
<p>Answer:</p>
|
<p>Answer:</p>
|
||||||
<div id="answer">Provide input first</div>
|
<div id="answer">Provide input first</div>
|
||||||
|
|||||||
242
script.js
242
script.js
@@ -1,18 +1,85 @@
|
|||||||
const ASSIGNMENT = 'assignment';
|
const ASSIGNMENT = 'assignment';
|
||||||
const ANSWER = 'answer';
|
const ANSWER = 'answer';
|
||||||
const ALGORITHM_CHECKBOX = 'algorithm';
|
|
||||||
const CONTAINER_NULL_CHARACTER=0;
|
|
||||||
const INSTRUCTION_FROM='from';
|
|
||||||
const INSTRUCTION_MOVE='move';
|
|
||||||
const INSTRUCTION_TO='to';
|
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,9 +88,7 @@ window.onload = function() {
|
|||||||
*/
|
*/
|
||||||
function calculateAnswer(event) {
|
function calculateAnswer(event) {
|
||||||
console.info("Calculating answer for input...");
|
console.info("Calculating answer for input...");
|
||||||
let assignment = document.getElementById(ASSIGNMENT).value;
|
let answer = algorithm(event.target.value);
|
||||||
let allAtOnce = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
|
||||||
let answer = algorithm(assignment, allAtOnce);
|
|
||||||
|
|
||||||
document.getElementById(ANSWER).innerText = answer;
|
document.getElementById(ANSWER).innerText = answer;
|
||||||
}
|
}
|
||||||
@@ -31,137 +96,52 @@ function calculateAnswer(event) {
|
|||||||
/**
|
/**
|
||||||
* Calculate the answer to assignment.
|
* Calculate the answer to assignment.
|
||||||
* @param assignment the input from the assignment.
|
* @param assignment the input from the assignment.
|
||||||
* @param allAtOnce should all containers be moved at once.
|
|
||||||
* @return string the answer
|
* @return string the answer
|
||||||
*/
|
*/
|
||||||
function algorithm(assignment, allAtOnce) {
|
function algorithm(assignment) {
|
||||||
let lines = assignment.split(NEWLINE_CHARACTER);
|
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||||
let containerPlan = Array.from({length:(lines[0].length+1)/4}, () => [])
|
console.info("Linecount:" + lines.length);
|
||||||
console.info("Linecount:" + lines.length);
|
|
||||||
|
|
||||||
let parsec = true;
|
let root = new TreeNode('/');
|
||||||
for(let i=0; i<lines.length; i++) {
|
let currentdir = root;
|
||||||
if(parsec) {
|
|
||||||
|
|
||||||
// Parse containers
|
for(let i=0; i<lines.length; i++) {
|
||||||
let containerLine = lines[i];
|
let terminalLine = lines[i].trim();
|
||||||
|
|
||||||
// Skip numbers
|
if(terminalLine.substring(0, 4) == "$ cd") {
|
||||||
if(containerLine[1] == '1') {
|
let changeDirectory = terminalLine.substring(5).trim();
|
||||||
parsec = false;
|
if(changeDirectory == "..") {
|
||||||
i+=1;
|
console.debug("One directory up from: " + currentdir.value);
|
||||||
console.table(containerPlan);
|
currentdir = currentdir.parent;
|
||||||
continue;
|
console.debug("navigates to: " + currentdir.value);
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let containers = parseContainers(containerLine);
|
if(changeDirectory == "/") {
|
||||||
if(containers == -1) {
|
currentdir = root;
|
||||||
// invalid containerline
|
continue;
|
||||||
// TODO: error
|
}
|
||||||
console.error("Container parse failure.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add containers
|
if(!currentdir.contains(changeDirectory)) {
|
||||||
//console.debug(containers);
|
currentdir.descendants.push(new TreeNode(changeDirectory, currentdir));
|
||||||
for(let j=0; j<containers.length; j++) {
|
}
|
||||||
let container = containers[j];
|
|
||||||
//console.debug(container);
|
|
||||||
if(container != CONTAINER_NULL_CHARACTER) {
|
|
||||||
//console.debug(j);
|
|
||||||
//console.debug(containerPlan);
|
|
||||||
containerPlan[j].push(container);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//console.table(containerPlan);
|
|
||||||
} else {
|
|
||||||
// Parse instructions
|
|
||||||
let instructionLine = lines[i];
|
|
||||||
let instruction = parseInstruction(instructionLine);
|
|
||||||
|
|
||||||
console.debug(instruction);
|
currentdir = currentdir.getDir(changeDirectory);
|
||||||
if(instruction == -1) {
|
} else if(terminalLine.substring(0, 3) != 'dir' && !terminalLine.includes('$')) {
|
||||||
// invalid instructionline
|
console.debug(terminalLine);
|
||||||
// TODO: error
|
spaceIndex = terminalLine.indexOf(' ');
|
||||||
console.error("Instruction parse failure.");
|
size = parseInt(terminalLine.substring(0, spaceIndex));
|
||||||
continue;
|
currentdir.increaseSize(size);
|
||||||
}
|
|
||||||
|
|
||||||
// Execute instruction
|
|
||||||
let movecount=0;
|
|
||||||
if(allAtOnce) {
|
|
||||||
while(instruction.move > 0) {
|
|
||||||
let container = containerPlan[instruction.from].shift();
|
|
||||||
containerPlan[instruction.to].unshift(container);
|
|
||||||
instruction.move--;
|
|
||||||
movecount++;
|
|
||||||
}
|
|
||||||
console.debug("moved:" + movecount);
|
|
||||||
} else {
|
|
||||||
let splicedContainers = containerPlan[instruction.from].splice(0, instruction.move);
|
|
||||||
containerPlan[instruction.to].unshift(...splicedContainers);
|
|
||||||
}
|
|
||||||
|
|
||||||
//console.table(containerPlan);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let result="";
|
let freeDiskSpace = TOTAL_SPACE - root.size;
|
||||||
for(let k=0; k<containerPlan.length; k++) {
|
console.debug("Free disk space: " + freeDiskSpace);
|
||||||
result += containerPlan[k][0];
|
|
||||||
}
|
|
||||||
|
|
||||||
return "Top containers on the stack are: " + result;
|
console.debug("Root size:" + root.size);
|
||||||
}
|
console.debug("Value to find: " + (UPDATE_SIZE - freeDiskSpace));
|
||||||
|
let closestNode = root.findClosestNodeToSize(UPDATE_SIZE - freeDiskSpace);
|
||||||
function parseInstruction(instructionLine) {
|
console.debug("Closest value: " + closestNode.size);
|
||||||
let instruction={
|
|
||||||
move:0,
|
return "Sum below maxSize " + MAX_SIZE + ": " + root.getSum(MAX_SIZE);
|
||||||
from:0,
|
|
||||||
to:0,
|
|
||||||
}
|
|
||||||
|
|
||||||
instructionLine.replace(/\s/g, ''); // Remove whitespace
|
|
||||||
let fromIndex = instructionLine.indexOf(INSTRUCTION_FROM);
|
|
||||||
let moveIndex = instructionLine.indexOf(INSTRUCTION_MOVE);
|
|
||||||
let toIndex = instructionLine.indexOf(INSTRUCTION_TO);
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
if(fromIndex < 0 || moveIndex < 0 || toIndex < 0) {
|
|
||||||
// Error
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO validate check for numbers after indices.
|
|
||||||
|
|
||||||
// Parse
|
|
||||||
instruction.move = parseInt(instructionLine.substring(moveIndex + INSTRUCTION_MOVE.length + 1, fromIndex));
|
|
||||||
instruction.from = parseInt(instructionLine.substring(fromIndex + INSTRUCTION_FROM.length + 1, toIndex)) -1;
|
|
||||||
instruction.to = parseInt(instructionLine.substring(toIndex + INSTRUCTION_TO.length + 1)) -1;
|
|
||||||
|
|
||||||
return instruction;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseContainers(containerLine) {
|
|
||||||
let containerLineLength = (containerLine.length+1);
|
|
||||||
//console.debug("Parse ContainerLine, length: " + containerLineLength);
|
|
||||||
|
|
||||||
if(!containerLine.includes('[') || !containerLine.includes(']') || containerLineLength % 4 != 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let containers = [];
|
|
||||||
for(let i=0; i<containerLineLength; i+=4) {
|
|
||||||
let containerLineSubString = containerLine.substring(i, i+4).trim();
|
|
||||||
let character = containerLineSubString[1];
|
|
||||||
if(containerLineSubString[0] != '[' || containerLineSubString[2] != ']') {
|
|
||||||
// Warning invalid container format
|
|
||||||
// TODO: warning
|
|
||||||
}
|
|
||||||
|
|
||||||
containers.push((!character)? CONTAINER_NULL_CHARACTER: character);
|
|
||||||
}
|
|
||||||
//console.debug(containers);
|
|
||||||
|
|
||||||
return containers;
|
|
||||||
}
|
}
|
||||||
@@ -6,10 +6,6 @@ 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