Solution for assignment3.
This commit is contained in:
126
script.js
126
script.js
@@ -1,5 +1,10 @@
|
||||
const ASSIGNMENT = 'assignment';
|
||||
const ANSWER = 'answer';
|
||||
const ALGORITHM_CHECKBOX = 'algorithm';
|
||||
const CHARCODE_START=64;
|
||||
const CHARCODE_UPPER_CORRECTION=6;
|
||||
const ERROR_MESSAGE_INVALID_INPUT_SIZE = "Invalid input size, should be higher than 2 for: ";
|
||||
const ERROR_MESSAGE_INVALID_ITEM_CHARACTER = "Invalid item character found: ";
|
||||
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 byCompartiments = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
||||
let answer = algorithm(assignment, byCompartiments);
|
||||
|
||||
document.getElementById(ANSWER).innerText = answer;
|
||||
}
|
||||
@@ -25,9 +33,121 @@ function calculateAnswer(event) {
|
||||
* @param assignment the input from the assignment.
|
||||
* @return string the answer
|
||||
*/
|
||||
function algorithm(assignment) {
|
||||
function algorithm(assignment, byCompartiments) {
|
||||
let priorityScoreSum = 0;
|
||||
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||
console.info("Linecount:" + lines.length);
|
||||
|
||||
// TODO: implement assignment
|
||||
let increment = (byCompartiments)? 1:3;
|
||||
for(let i=0; i<lines.length; i+=increment) {
|
||||
let inputs = [];
|
||||
let rucksack = lines[i].replace(/\s/g, ''); // Remove whitespace
|
||||
|
||||
if(byCompartiments) {
|
||||
let compartiment1 = rucksack.slice(0, rucksack.length/2);
|
||||
let compartiment2 = rucksack.slice(rucksack.length/2);
|
||||
inputs = [compartiment1, compartiment2];
|
||||
} else {
|
||||
inputs = [rucksack, lines[i+1].replace(/\s/g, ''), lines[i+2].replace(/\s/g, '')];
|
||||
}
|
||||
|
||||
let duplicate = getDuplicateCharacter(inputs);
|
||||
if(duplicate < 0) {
|
||||
console.error(ERROR_MESSAGE_INVALID_INPUT_SIZE + inputs + " on line: " + i);
|
||||
}
|
||||
|
||||
let priorityScore = getPriorityScore(duplicate);
|
||||
if(priorityScore < 0) {
|
||||
console.error(ERROR_MESSAGE_INVALID_ITEM_CHARACTER + duplicate + " on line: " + i);
|
||||
continue;
|
||||
}
|
||||
priorityScoreSum += priorityScore;
|
||||
}
|
||||
|
||||
return "SUM priorityscore: " + priorityScoreSum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the answer to assignment.
|
||||
* @param assignment the input from the assignment.
|
||||
* @return string the answer
|
||||
*/
|
||||
function algorithm1(assignment) {
|
||||
let priorityScoreSum = 0;
|
||||
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||
console.info("Linecount:" + lines.length);
|
||||
|
||||
for(let i=0; i<lines.length; i++) {
|
||||
let rucksack = lines[i].replace(/\s/g, ''); // Remove whitespace
|
||||
let compartiment1 = rucksack.slice(0, rucksack.length/2);
|
||||
let compartiment2 = rucksack.slice(rucksack.length/2);
|
||||
|
||||
console.debug("Check rucksack" + i, ": " + rucksack + " \n with compartiment1: " + compartiment1 + "\n and compartiment2: " + compartiment2);
|
||||
|
||||
for(item of compartiment2) {
|
||||
if(compartiment1.includes(item)) {
|
||||
console.debug("Found duplicate: " + item + " in rucksack" + i);
|
||||
let priorityScore = getPriorityScore(item);
|
||||
if(priorityScore < 0) {
|
||||
console.error(ERROR_MESSAGE_INVALID_ITEM_CHARACTER + item + " on line: " + i);
|
||||
continue;
|
||||
}
|
||||
priorityScoreSum += priorityScore;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "SUM priorityscore: " + priorityScoreSum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find duplicate character in inputs.
|
||||
* @param string[] inputs the inputs to find duplicates in.
|
||||
* @return char the duplicate character
|
||||
*/
|
||||
function getDuplicateCharacter(inputs) {
|
||||
console.debug("Checking inputs: " + inputs);
|
||||
if(inputs.length < 2) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(let item of inputs[0]) {
|
||||
let duplicate = true
|
||||
for(let j=1; j<inputs.length; j++) {
|
||||
duplicate = duplicate && inputs[j].includes(item);
|
||||
}
|
||||
|
||||
if(duplicate) {
|
||||
console.debug("Found duplicate: " + item);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns priorityscore for given character.
|
||||
* @param char character the character
|
||||
* @return int the priorityscore for character or -1 if invalid
|
||||
*/
|
||||
function getPriorityScore(character) {
|
||||
let result=0;
|
||||
let uppercase = character.toUpperCase();
|
||||
let lowercase = character.toLowerCase();
|
||||
|
||||
// validate character
|
||||
if( uppercase == lowercase ) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(character == uppercase) { // Switch results for upper and lowercase characters
|
||||
result -= CHARCODE_UPPER_CORRECTION;
|
||||
character = lowercase;
|
||||
} else {
|
||||
character = uppercase;
|
||||
}
|
||||
|
||||
result += character.charCodeAt() - CHARCODE_START;
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user