Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 312a514472 |
21
README.md
21
README.md
@@ -1,13 +1,14 @@
|
|||||||
# Advent of Code 2022 - Assignment5 javascript
|
# Advent of Code - assignment 9 javascript
|
||||||
This repository contains answers to the assignments of Advent of Coding 2022
|
This repository contains answers to the assignments of Advent of Coding 2022
|
||||||
|
|
||||||
## References
|
## References
|
||||||
- https://www.w3schools.com/jsref/jsref_includes.asp
|
- https://betterprogramming.pub/tuples-in-javascript-57ede9b1c9d2
|
||||||
- https://stackoverflow.com/questions/10261986/how-to-detect-string-which-contains-only-spaces
|
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length
|
||||||
- https://www.w3schools.com/jsref/jsref_indexof.asp
|
- https://www.javascripttutorial.net/javascript-multidimensional-array/
|
||||||
- https://stackoverflow.com/questions/8073673/how-can-i-add-new-array-elements-at-the-beginning-of-an-array-in-javascript
|
- https://flaviocopes.com/how-to-replace-whitespace-javascript/
|
||||||
- https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript
|
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
|
||||||
- https://www.w3schools.com/jsref/jsref_ceil.asp
|
- https://www.sohamkamani.com/javascript/enums/
|
||||||
- https://stackoverflow.com/questions/30561056/console-log-a-multi-dimensional-array
|
- https://www.w3schools.com/js/js_switch.asp
|
||||||
- https://teamtreehouse.com/community/removing-more-than-1-element-using-pop-and-shift-method
|
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
|
||||||
- https://stackoverflow.com/questions/14723848/push-multiple-elements-to-array
|
- https://stackoverflow.com/questions/4564414/delete-first-character-of-string-if-it-is-0
|
||||||
|
- https://stackoverflow.com/questions/1959040/is-it-possible-to-send-a-variable-number-of-arguments-to-a-javascript-function
|
||||||
@@ -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>
|
||||||
|
|||||||
274
script.js
274
script.js
@@ -1,10 +1,5 @@
|
|||||||
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';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,18 +7,136 @@ const NEWLINE_CHARACTER = '\n';
|
|||||||
*/
|
*/
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DIRECTIONS = {
|
||||||
|
UP: 'U',
|
||||||
|
DOWN: 'D',
|
||||||
|
LEFT: 'L',
|
||||||
|
RIGHT: 'R'
|
||||||
|
};
|
||||||
|
|
||||||
|
class Knot {
|
||||||
|
constructor(x, y) {
|
||||||
|
this.x=x;
|
||||||
|
this.y=y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*class Rope {
|
||||||
|
constructor(headX, headY, tailX, tailY) {
|
||||||
|
this.headX = headX;
|
||||||
|
this.headY = headY;
|
||||||
|
this.tailX = tailX;
|
||||||
|
this.tailY = tailY;
|
||||||
|
console.info("Created Rope with head:" + headX + "," + headY + " and tail: " + tailX + "," + tailY);
|
||||||
|
}
|
||||||
|
|
||||||
|
step = function (direction) {
|
||||||
|
let oldHeadX = this.headX;
|
||||||
|
let oldHeadY = this.headY;
|
||||||
|
switch (direction) {
|
||||||
|
case DIRECTIONS.UP:
|
||||||
|
this.headY++;
|
||||||
|
break;
|
||||||
|
case DIRECTIONS.DOWN:
|
||||||
|
this.headY--;
|
||||||
|
break;
|
||||||
|
case DIRECTIONS.LEFT:
|
||||||
|
this.headX--;
|
||||||
|
break;
|
||||||
|
case DIRECTIONS.RIGHT:
|
||||||
|
this.headX++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error("Invalid direction given: " + direction);
|
||||||
|
}
|
||||||
|
this.__correctTail(oldHeadX, oldHeadY);
|
||||||
|
}
|
||||||
|
|
||||||
|
getTailPosition = function () {
|
||||||
|
return "" + this.tailX + "," + this.tailY;
|
||||||
|
}
|
||||||
|
|
||||||
|
__correctTail = function (oldHeadX, oldHeadY) {
|
||||||
|
let absoluteDifferenceX = Math.abs(this.headX - this.tailX);
|
||||||
|
let absoluteDifferenceY = Math.abs(this.headY - this.tailY);
|
||||||
|
|
||||||
|
if (absoluteDifferenceX > 1 || absoluteDifferenceY > 1) {
|
||||||
|
this.tailX = oldHeadX;
|
||||||
|
this.tailY = oldHeadY;
|
||||||
|
console.debug("Corrected tail Position: (" + this.tailX + ", " + this.tailY + "), head position: (" + this.headX + ", " + this.headY + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};*/
|
||||||
|
class Rope {
|
||||||
|
constructor(count) {
|
||||||
|
if(count < 2) {
|
||||||
|
console.error("Rope needs at least 2 knots.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let knots = [];
|
||||||
|
while(count > 0) {
|
||||||
|
knots.push(new Knot(0,0));
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.head = knots[0];
|
||||||
|
this.knots = Array.from(knots).slice(1);
|
||||||
|
this.tail = this.knots[this.knots.length-1];
|
||||||
|
console.info("Created Rope with " + knots.length + " knots.");
|
||||||
|
}
|
||||||
|
|
||||||
|
step = function(direction) {
|
||||||
|
let oldHeadX = this.head.x;
|
||||||
|
let oldHeadY = this.head.y;
|
||||||
|
switch(direction) {
|
||||||
|
case DIRECTIONS.UP:
|
||||||
|
this.head.y++;
|
||||||
|
break;
|
||||||
|
case DIRECTIONS.DOWN:
|
||||||
|
this.head.y--;
|
||||||
|
break;
|
||||||
|
case DIRECTIONS.LEFT:
|
||||||
|
this.head.x--;
|
||||||
|
break;
|
||||||
|
case DIRECTIONS.RIGHT:
|
||||||
|
this.head.x++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.error("Invalid direction given: " + direction);
|
||||||
|
}
|
||||||
|
this.__correctTails(oldHeadX, oldHeadY, this.head, this.knots[0], this.knots.slice(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
getTailPosition = function() {
|
||||||
|
return "" + this.tail.x + "," + this.tail.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
__correctTails = function(oldHeadX, oldHeadY, head, tail, tails) {
|
||||||
|
let absoluteDifferenceX = Math.abs(head.x - tail.x);
|
||||||
|
let absoluteDifferenceY = Math.abs(head.y - tail.y);
|
||||||
|
let oldTailX = tail.x;
|
||||||
|
let oldTailY = tail.y;
|
||||||
|
|
||||||
|
if(absoluteDifferenceX > 1 || absoluteDifferenceY > 1) {
|
||||||
|
tail.x = oldHeadX;
|
||||||
|
tail.y = oldHeadY;
|
||||||
|
console.debug("Corrected tail Position: (" + this.tail.x + ", " + this.tail.y + "), head position: (" + this.head.x + ", " + this.head.y + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(tails.length > 0)
|
||||||
|
this.__correctTails(oldTailX, oldTailY, tail, tails[0], tails.slice(1));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listener function for input in assignment field.
|
* Listener function for input in assignment field.
|
||||||
* @param event the onInput event
|
* @param event the onInput event
|
||||||
*/
|
*/
|
||||||
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 +144,36 @@ 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 rope = new Rope(10);
|
||||||
|
let visitedPositions = [rope.getTailPosition()];
|
||||||
|
|
||||||
for(let i=0; i<lines.length; i++) {
|
for(let i=0; i<lines.length; i++) {
|
||||||
if(parsec) {
|
let line = lines[i].replace(/\s/g, ''); // Remove all whitespace
|
||||||
|
let direction = line[0];
|
||||||
|
let count = parseInt(line.substring(1));
|
||||||
|
|
||||||
|
if(isNaN(count)) { // Validate count
|
||||||
|
console.error("Invalid number in line: " + lines[i]);
|
||||||
|
}
|
||||||
|
|
||||||
// Parse containers
|
while(count > 0) {
|
||||||
let containerLine = lines[i];
|
rope.step(direction);
|
||||||
|
let tailPosition = rope.getTailPosition();
|
||||||
|
|
||||||
// Skip numbers
|
if(!visitedPositions.includes(tailPosition) ) { // Add only unique tailpositions
|
||||||
if(containerLine[1] == '1') {
|
visitedPositions.push(tailPosition);
|
||||||
parsec = false;
|
|
||||||
i+=1;
|
|
||||||
console.table(containerPlan);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let containers = parseContainers(containerLine);
|
|
||||||
if(containers == -1) {
|
|
||||||
// invalid containerline
|
|
||||||
// TODO: error
|
|
||||||
console.error("Container parse failure.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add containers
|
|
||||||
//console.debug(containers);
|
|
||||||
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);
|
|
||||||
if(instruction == -1) {
|
|
||||||
// invalid instructionline
|
|
||||||
// TODO: error
|
|
||||||
console.error("Instruction parse failure.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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="";
|
|
||||||
for(let k=0; k<containerPlan.length; k++) {
|
|
||||||
result += containerPlan[k][0];
|
|
||||||
}
|
|
||||||
|
|
||||||
return "Top containers on the stack are: " + result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseInstruction(instructionLine) {
|
|
||||||
let instruction={
|
|
||||||
move:0,
|
|
||||||
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);
|
count--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//console.debug(containers);
|
|
||||||
|
|
||||||
return containers;
|
console.log(visitedPositions);
|
||||||
|
return "Amount of positions for tail: " + visitedPositions.length;
|
||||||
}
|
}
|
||||||
@@ -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