1 Commits

Author SHA1 Message Date
28597a3f26 Initial working commit 2022-12-11 12:44:35 +01:00
4 changed files with 112 additions and 57 deletions

View File

@@ -1,17 +1,8 @@
# Advent of Code 2022 - assignment 1 # Advent of Code 2022 - Assignment8 javascript
This is the first assignment for Advent of Code 2022. The provided solution uses 3 variables: currentScore, highestCalorieScore, currentElf, highestElf. ## Description
It is complexity O(n+m) since it loops through every line exactly once (n) and the iterates for recalculating highscore (m). This problem is all about walking through a grid. In this solution, we look through an entire column and row until we find a node with height <= height.
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://www.w3schools.com/jsref/jsref_charat.asp
- https://stackabuse.com/javascript-check-if-variable-is-a-number/
## references - https://www.w3schools.com/jsref/jsref_abs.asp
- https://adventofcode.com/2022/day/1
- https://stackoverflow.com/questions/17595361/how-to-read-text-line-by-line-in-a-html-text-area
- https://bobbyhadz.com/blog/javascript-get-value-of-textarea
- https://stackoverflow.com/questions/154059/how-do-i-check-for-an-empty-undefined-null-string-in-javascript
- 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

View File

@@ -8,8 +8,8 @@
<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"/> <input type="checkbox" id="algorithm" />
<label for="algorithm">TOP 3</label> <label for="algorithm">Highest scenic score?</label>
<p>Answer:</p> <p>Answer:</p>
<div id="answer">Provide input first</div> <div id="answer">Provide input first</div>

130
script.js
View File

@@ -2,13 +2,11 @@ const ASSIGNMENT = 'assignment';
const ANSWER = 'answer'; const ANSWER = 'answer';
const ALGORITHM_CHECKBOX = 'algorithm'; 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); document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", calculateAnswer);
} }
@@ -20,51 +18,117 @@ 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 getHighestScenicScore = document.getElementById(ALGORITHM_CHECKBOX).checked;
let answer = algorithm(assignment, (top3)? TOP_N: 1); let answer = algorithm(assignment, getHighestScenicScore);
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, getHighestScenicScore) {
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 visibleTreeCount = 0;
let line = lines[i]; let highestScenicScore = 0;
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 row = lines[i].trim();
for(let j=0; j < highestCalorieScores.length; j++) { for (let j = 0; j < row.length; j++) {
if(currentCalorieScore > highestCalorieScores[j]) { if (i <= 0 || i >= lines.length - 1 || j <= 0 || j >= row.length - 1) { // Edge node
console.debug("New highest #"+(j+1) + " elf found, with score:" + currentCalorieScore); //console.debug("Edge node found.");
let oldHighestCalorieScore = highestCalorieScores[j]; visibleTreeCount++;
highestCalorieScores[j] = currentCalorieScore;
currentCalorieScore = oldHighestCalorieScore;
}
}
// Reset values
currentCalorieScore = 0;
currentElf++;
continue; continue;
} }
let calorieScore = parseInt(line); let height = parseInt(lines[i].charAt(j));
currentCalorieScore += calorieScore; if (!walkRowVisible(height, j, row) && !walkColumnVisible(height, i, j, lines)) {
console.log("Found invisible tree, height:" + height + ", i:" + i + ", j:" + j);
visibleTreeCount--;
} }
console.debug(highestCalorieScores); if (getHighestScenicScore) {
return "The highest of sum of calorie amount: " + highestCalorieScores.reduce(function(a, b) { return a + b; }, 0); let scenicScore = walkRowCount(height, j, row) * walkColumnCount(height, i, j, lines);
if (scenicScore > highestScenicScore) {
console.debug("Found new highest scenic score: " + scenicScore);
highestScenicScore = scenicScore;
}
}
// Tree is on edge.
visibleTreeCount++;
}
}
if (getHighestScenicScore) {
return "Highest scenic score: " + highestScenicScore;
}
return "Amount of visible trees in the grid: " + visibleTreeCount;
}
function walkColumnCount(height, index, rowIndex, column) {
return walkCount(height, index, column, false, rowIndex);
}
function walkRowCount(height, index, row) {
return walkCount(height, index, row, true, -1);
}
function walkColumnVisible(height, index, rowIndex, column) {
return walkVisible(height, index, column, false, rowIndex);
}
function walkRowVisible(height, index, row) {
return walkVisible(height, index, row, true, -1);
}
function walkCount(height, index, line, isRow, rowIndex) {
let visibleLeftCount = __walkHelper(height, index, line, isRow, -1, rowIndex, 0);
let visibleRightCount = __walkHelper(height, index, line, isRow, 1, rowIndex, 0);
return visibleLeftCount * visibleRightCount;
}
function walkVisible(height, index, line, isRow, rowIndex) {
let visibleLeft = __walkHelper(height, index, line, isRow, -1, rowIndex);
let visibleRight = __walkHelper(height, index, line, isRow, 1, rowIndex);
return visibleLeft || visibleRight;
}
function __walkHelper(height, index, line, isRow, direction, rowIndex, count) {
if (direction == 0) {
console.error("Invalid direction 0 in __walkhelper");
return -1;
}
direction = direction / Math.abs(direction);
// Edge reached
if ((direction < 0 && index <= 0) || (direction > 0 && index >= line.length - 1)) {
return (count == undefined) ? true : count;
}
let nextIndex = index + direction;
let nextHeightCharacter = (isRow) ? line.charAt(nextIndex) : line[nextIndex].trim().charAt(rowIndex);
let nextHeight = parseInt(nextHeightCharacter);
if (isNaN(height) || isNaN(nextHeightCharacter)) {
console.error("Invalid height found, height:" + height + ", nextHeight: " + nextHeightCharacter);
return -1;
}
if (height <= nextHeight) {
if (count == undefined)
return false;
return count + 1;
}
return __walkHelper(height, nextIndex, line, isRow, direction, rowIndex, (count !=undefined)? count + 1 : undefined);
} }