diff --git a/README.md b/README.md index da57339..4f64e69 100644 --- a/README.md +++ b/README.md @@ -1 +1,13 @@ +# Advent of Code 2022 - Assignment5 javascript This repository contains answers to the assignments of Advent of Coding 2022 + +## References +- https://www.w3schools.com/jsref/jsref_includes.asp +- https://stackoverflow.com/questions/10261986/how-to-detect-string-which-contains-only-spaces +- 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 \ No newline at end of file diff --git a/index.html b/index.html index 5cf1ba4..f34b3fe 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,8 @@

Assignment:

+ +

Answer:

Provide input first
diff --git a/script.js b/script.js index a6e2848..374dda0 100644 --- a/script.js +++ b/script.js @@ -1,5 +1,10 @@ const ASSIGNMENT = 'assignment'; 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'; /** @@ -7,6 +12,7 @@ const NEWLINE_CHARACTER = '\n'; */ window.onload = function() { document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer); + document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", calculateAnswer); } /** @@ -15,7 +21,9 @@ window.onload = function() { */ function calculateAnswer(event) { console.info("Calculating answer for input..."); - let answer = algorithm(event.target.value); + let assignment = document.getElementById(ASSIGNMENT).value; + let allAtOnce = document.getElementById(ALGORITHM_CHECKBOX).checked; + let answer = algorithm(assignment, allAtOnce); document.getElementById(ANSWER).innerText = answer; } @@ -23,11 +31,137 @@ function calculateAnswer(event) { /** * Calculate the answer to assignment. * @param assignment the input from the assignment. + * @param allAtOnce should all containers be moved at once. * @return string the answer */ -function algorithm(assignment) { - let lines = assignment.trim().split(NEWLINE_CHARACTER); +function algorithm(assignment, allAtOnce) { + let lines = assignment.split(NEWLINE_CHARACTER); + let containerPlan = Array.from({length:(lines[0].length+1)/4}, () => []) console.info("Linecount:" + lines.length); - // TODO: implement assignment + let parsec = true; + for(let i=0; i 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=""; + for(let k=0; k