179 lines
4.6 KiB
JavaScript
179 lines
4.6 KiB
JavaScript
const ASSIGNMENT = 'assignment';
|
|
const ANSWER = 'answer';
|
|
const NEWLINE_CHARACTER = '\n';
|
|
|
|
/**
|
|
* Main function
|
|
*/
|
|
window.onload = function() {
|
|
document.getElementById(ASSIGNMENT).addEventListener("input", 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.
|
|
* @param event the onInput event
|
|
*/
|
|
function calculateAnswer(event) {
|
|
console.info("Calculating answer for input...");
|
|
let answer = algorithm(event.target.value);
|
|
|
|
document.getElementById(ANSWER).innerText = answer;
|
|
}
|
|
|
|
/**
|
|
* Calculate the answer to assignment.
|
|
* @param assignment the input from the assignment.
|
|
* @return string the answer
|
|
*/
|
|
function algorithm(assignment) {
|
|
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
|
console.info("Linecount:" + lines.length);
|
|
|
|
let rope = new Rope(10);
|
|
let visitedPositions = [rope.getTailPosition()];
|
|
|
|
for(let i=0; i<lines.length; i++) {
|
|
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]);
|
|
}
|
|
|
|
while(count > 0) {
|
|
rope.step(direction);
|
|
let tailPosition = rope.getTailPosition();
|
|
|
|
if(!visitedPositions.includes(tailPosition) ) { // Add only unique tailpositions
|
|
visitedPositions.push(tailPosition);
|
|
}
|
|
|
|
count--;
|
|
}
|
|
}
|
|
|
|
console.log(visitedPositions);
|
|
return "Amount of positions for tail: " + visitedPositions.length;
|
|
} |