Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| df5b34216c |
27
README.md
27
README.md
@@ -1,17 +1,12 @@
|
|||||||
# Advent of Code 2022 - assignment 1
|
# Advent of Code 2022 - Assignment3 javascript solution
|
||||||
This is the first assignment for Advent of Code 2022. The provided solution uses 3 variables: currentScore, highestCalorieScore, currentElf, highestElf.
|
This problem is all about searching for duplicates in strings. The solution is implemented using slice to split the string in half.
|
||||||
It is complexity O(n+m) since it loops through every line exactly once (n) and the iterates for recalculating highscore (m).
|
The priority is retrieved using the ASCII charcode character for convienence. How to retrieve inputs differs between part1 and 2, therefore a checkbox is provided to toggle between different parsing methods.
|
||||||
The GUI also provides a checkbox to toggle top3 or top1.
|
|
||||||
|
|
||||||
## Solution description
|
## References
|
||||||
Every iteration the score is added to the currentScore. If an empty line is detected we check if currentscore exceeds one of our highestCalorieScore, if yes we assign currentElf to highestElf.
|
- https://stackoverflow.com/questions/94037/convert-character-to-ascii-code-in-javascript
|
||||||
|
- https://stackoverflow.com/questions/20474257/split-string-into-two-parts
|
||||||
## references
|
- https://www.folkstalk.com/2022/10/find-common-characters-in-two-strings-javascript-with-code-examples.html
|
||||||
- https://adventofcode.com/2022/day/1
|
- https://stackoverflow.com/questions/351409/how-to-append-something-to-an-array
|
||||||
- https://stackoverflow.com/questions/17595361/how-to-read-text-line-by-line-in-a-html-text-area
|
- https://stackoverflow.com/questions/9862761/how-to-check-if-character-is-a-letter-in-javascript
|
||||||
- https://bobbyhadz.com/blog/javascript-get-value-of-textarea
|
- https://stackoverflow.com/questions/1966476/how-can-i-process-each-letter-of-text-using-javascript
|
||||||
- https://stackoverflow.com/questions/154059/how-do-i-check-for-an-empty-undefined-null-string-in-javascript
|
- https://www.w3schools.com/js/js_arrays.asp
|
||||||
- https://www.geeksforgeeks.org/convert-a-string-to-an-integer-in-javascript/#:~:text=In%20JavaScript%20parseInt()%20function,argument%20of%20parseInt()%20function.
|
|
||||||
- https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers
|
|
||||||
- https://www.w3schools.com/js/js_break.asp
|
|
||||||
- https://2ality.com/2018/12/creating-arrays.html
|
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
<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"/>
|
<input type="checkbox" id="algorithm"/>
|
||||||
<label for="algorithm">TOP 3</label>
|
<label for="algorithm">Solution by compartiment.</label>
|
||||||
|
|
||||||
<p>Answer:</p>
|
<p>Answer:</p>
|
||||||
<div id="answer">Provide input first</div>
|
<div id="answer">Provide input first</div>
|
||||||
|
|||||||
151
script.js
151
script.js
@@ -1,9 +1,11 @@
|
|||||||
const ASSIGNMENT = 'assignment';
|
const ASSIGNMENT = 'assignment';
|
||||||
const ANSWER = 'answer';
|
const ANSWER = 'answer';
|
||||||
const ALGORITHM_CHECKBOX = 'algorithm';
|
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';
|
const NEWLINE_CHARACTER = '\n';
|
||||||
const TOP_1 = 1;
|
|
||||||
const TOP_N = 3;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function
|
* Main function
|
||||||
@@ -20,51 +22,132 @@ 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 assignment = document.getElementById(ASSIGNMENT).value;
|
||||||
let top3 = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
let byCompartiments = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
||||||
let answer = algorithm(assignment, (top3)? TOP_N: 1);
|
let answer = algorithm(assignment, byCompartiments);
|
||||||
|
|
||||||
document.getElementById(ANSWER).innerText = answer;
|
document.getElementById(ANSWER).innerText = answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the answer to assignment1 with 2 variables, count and highest.
|
* Calculate the answer to assignment.
|
||||||
* @param assignment the input from the assignment.
|
* @param assignment the input from the assignment.
|
||||||
* @param topn the
|
* @return string the answer
|
||||||
* @return string the elf with the highest calory count
|
|
||||||
*/
|
*/
|
||||||
function algorithm(assignment, topn) {
|
function algorithm(assignment, byCompartiments) {
|
||||||
let currentCalorieScore = 0;
|
let priorityScoreSum = 0;
|
||||||
let highestCalorieScores = Array.from({length: topn}, () => (0));
|
|
||||||
let currentElf = 1;
|
|
||||||
|
|
||||||
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||||
console.info("Linecount:" + lines.length);
|
console.info("Linecount:" + lines.length);
|
||||||
|
|
||||||
for(var i = 0; i < lines.length; i++){
|
let increment = (byCompartiments)? 1:3;
|
||||||
let line = lines[i];
|
for(let i=0; i<lines.length; i+=increment) {
|
||||||
|
let inputs = [];
|
||||||
|
let rucksack = lines[i].replace(/\s/g, ''); // Remove whitespace
|
||||||
|
|
||||||
if(!line && currentCalorieScore > 0) { // Check for empty line and skip subsequent emptylines
|
if(byCompartiments) {
|
||||||
console.debug("Calculated score for elf " + currentElf + ": " + currentCalorieScore);
|
let compartiment1 = rucksack.slice(0, rucksack.length/2);
|
||||||
|
let compartiment2 = rucksack.slice(rucksack.length/2);
|
||||||
for(let j=0; j < highestCalorieScores.length; j++) {
|
inputs = [compartiment1, compartiment2];
|
||||||
if(currentCalorieScore > highestCalorieScores[j]) {
|
} else {
|
||||||
console.debug("New highest #"+(j+1) + " elf found, with score:" + currentCalorieScore);
|
inputs = [rucksack, lines[i+1].replace(/\s/g, ''), lines[i+2].replace(/\s/g, '')];
|
||||||
let oldHighestCalorieScore = highestCalorieScores[j];
|
|
||||||
highestCalorieScores[j] = currentCalorieScore;
|
|
||||||
currentCalorieScore = oldHighestCalorieScore;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset values
|
|
||||||
currentCalorieScore = 0;
|
|
||||||
currentElf++;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let calorieScore = parseInt(line);
|
let duplicate = getDuplicateCharacter(inputs);
|
||||||
currentCalorieScore += calorieScore;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.debug(highestCalorieScores);
|
return "SUM priorityscore: " + priorityScoreSum;
|
||||||
return "The highest of sum of calorie amount: " + highestCalorieScores.reduce(function(a, b) { return a + b; }, 0);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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