Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 312a514472 |
29
README.md
29
README.md
@@ -1,17 +1,14 @@
|
|||||||
# Advent of Code 2022 - assignment 1
|
# Advent of Code - assignment 9 javascript
|
||||||
This is the first assignment for Advent of Code 2022. The provided solution uses 3 variables: currentScore, highestCalorieScore, currentElf, highestElf.
|
This repository contains answers to the assignments of Advent of Coding 2022
|
||||||
It is complexity O(n+m) since it loops through every line exactly once (n) and the iterates for recalculating highscore (m).
|
|
||||||
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://betterprogramming.pub/tuples-in-javascript-57ede9b1c9d2
|
||||||
|
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length
|
||||||
## references
|
- https://www.javascripttutorial.net/javascript-multidimensional-array/
|
||||||
- https://adventofcode.com/2022/day/1
|
- https://flaviocopes.com/how-to-replace-whitespace-javascript/
|
||||||
- https://stackoverflow.com/questions/17595361/how-to-read-text-line-by-line-in-a-html-text-area
|
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
|
||||||
- https://bobbyhadz.com/blog/javascript-get-value-of-textarea
|
- https://www.sohamkamani.com/javascript/enums/
|
||||||
- https://stackoverflow.com/questions/154059/how-do-i-check-for-an-empty-undefined-null-string-in-javascript
|
- https://www.w3schools.com/js/js_switch.asp
|
||||||
- https://www.geeksforgeeks.org/convert-a-string-to-an-integer-in-javascript/#:~:text=In%20JavaScript%20parseInt()%20function,argument%20of%20parseInt()%20function.
|
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
|
||||||
- https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers
|
- https://stackoverflow.com/questions/4564414/delete-first-character-of-string-if-it-is-0
|
||||||
- https://www.w3schools.com/js/js_break.asp
|
- https://stackoverflow.com/questions/1959040/is-it-possible-to-send-a-variable-number-of-arguments-to-a-javascript-function
|
||||||
- https://2ality.com/2018/12/creating-arrays.html
|
|
||||||
@@ -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">TOP 3</label>
|
|
||||||
|
|
||||||
<p>Answer:</p>
|
<p>Answer:</p>
|
||||||
<div id="answer">Provide input first</div>
|
<div id="answer">Provide input first</div>
|
||||||
|
|||||||
183
script.js
183
script.js
@@ -1,70 +1,179 @@
|
|||||||
const ASSIGNMENT = 'assignment';
|
const ASSIGNMENT = 'assignment';
|
||||||
const ANSWER = 'answer';
|
const ANSWER = 'answer';
|
||||||
const ALGORITHM_CHECKBOX = 'algorithm';
|
|
||||||
const NEWLINE_CHARACTER = '\n';
|
const NEWLINE_CHARACTER = '\n';
|
||||||
const TOP_1 = 1;
|
|
||||||
const TOP_N = 3;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function
|
* Main function
|
||||||
*/
|
*/
|
||||||
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 top3 = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
|
||||||
let answer = algorithm(assignment, (top3)? TOP_N: 1);
|
|
||||||
|
|
||||||
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) {
|
||||||
let currentCalorieScore = 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 rope = new Rope(10);
|
||||||
let line = lines[i];
|
let visitedPositions = [rope.getTailPosition()];
|
||||||
|
|
||||||
if(!line && currentCalorieScore > 0) { // Check for empty line and skip subsequent emptylines
|
for(let i=0; i<lines.length; i++) {
|
||||||
console.debug("Calculated score for elf " + currentElf + ": " + currentCalorieScore);
|
let line = lines[i].replace(/\s/g, ''); // Remove all whitespace
|
||||||
|
let direction = line[0];
|
||||||
|
let count = parseInt(line.substring(1));
|
||||||
|
|
||||||
for(let j=0; j < highestCalorieScores.length; j++) {
|
if(isNaN(count)) { // Validate count
|
||||||
if(currentCalorieScore > highestCalorieScores[j]) {
|
console.error("Invalid number in line: " + lines[i]);
|
||||||
console.debug("New highest #"+(j+1) + " elf found, with score:" + currentCalorieScore);
|
}
|
||||||
let oldHighestCalorieScore = highestCalorieScores[j];
|
|
||||||
highestCalorieScores[j] = currentCalorieScore;
|
while(count > 0) {
|
||||||
currentCalorieScore = oldHighestCalorieScore;
|
rope.step(direction);
|
||||||
|
let tailPosition = rope.getTailPosition();
|
||||||
|
|
||||||
|
if(!visitedPositions.includes(tailPosition) ) { // Add only unique tailpositions
|
||||||
|
visitedPositions.push(tailPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
count--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset values
|
console.log(visitedPositions);
|
||||||
currentCalorieScore = 0;
|
return "Amount of positions for tail: " + visitedPositions.length;
|
||||||
currentElf++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let calorieScore = parseInt(line);
|
|
||||||
currentCalorieScore += calorieScore;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.debug(highestCalorieScores);
|
|
||||||
return "The highest of sum of calorie amount: " + highestCalorieScores.reduce(function(a, b) { return a + b; }, 0);
|
|
||||||
}
|
}
|
||||||
@@ -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