1 Commits

Author SHA1 Message Date
7a3dd6bcde Assignment on my birthday 2022-12-06 17:26:38 +01:00
4 changed files with 28 additions and 107 deletions

View File

@@ -1,8 +1,5 @@
# Advent of Code 2022 - Assignment8 javascript # Advent of Code 2022 - Assignment6_javascript
## Description This repository contains answers to the assignments of Advent of Coding 2022
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.
## References ## References
- https://www.w3schools.com/jsref/jsref_charat.asp - https://stackoverflow.com/questions/28207610/checking-if-the-characters-in-a-string-are-all-unique
- https://stackabuse.com/javascript-check-if-variable-is-a-number/
- https://www.w3schools.com/jsref/jsref_abs.asp

View File

@@ -9,8 +9,8 @@
<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">Highest scenic score?</label> <label for="algorithm">Search for message marker?</label>
<p>Answer:</p> <p>Answer:</p>
<div id="answer">Provide input first</div> <div id="answer">Provide input first</div>
<script src="script.js"></script> <script src="script.js"></script>

120
script.js
View File

@@ -1,12 +1,14 @@
const ASSIGNMENT = 'assignment'; const ASSIGNMENT = 'assignment';
const ANSWER = 'answer'; const ANSWER = 'answer';
const ALGORITHM_CHECKBOX = 'algorithm'; const ALGORITHM_CHECKBOX = 'algorithm';
const MARKER_DISTINCT_CHARACTERS = 4;
const MESSAGE_DISTINCT_CHARACTERS = 14;
const NEWLINE_CHARACTER = '\n'; const NEWLINE_CHARACTER = '\n';
/** /**
* 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);
} }
@@ -18,8 +20,8 @@ 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 getHighestScenicScore = document.getElementById(ALGORITHM_CHECKBOX).checked; let distinctCharacters = (document.getElementById(ALGORITHM_CHECKBOX).checked)? MESSAGE_DISTINCT_CHARACTERS: MARKER_DISTINCT_CHARACTERS;
let answer = algorithm(assignment, getHighestScenicScore); let answer = algorithm(assignment, distinctCharacters);
document.getElementById(ANSWER).innerText = answer; document.getElementById(ANSWER).innerText = answer;
} }
@@ -29,106 +31,28 @@ function calculateAnswer(event) {
* @param assignment the input from the assignment. * @param assignment the input from the assignment.
* @return string the answer * @return string the answer
*/ */
function algorithm(assignment, getHighestScenicScore) { function algorithm(assignment, distinctCharacters) {
let lines = assignment.trim().split(NEWLINE_CHARACTER); let signal = assignment.trim();
console.info("Linecount:" + lines.length); let marker = assignment.substring(0,distinctCharacters);
let visibleTreeCount = 0; for(let i=distinctCharacters; i<signal.length; i++) {
let highestScenicScore = 0; if(containsDistinctCharacters(marker)) {
console.debug("Found marker after:" + i + " characters");
for (let i = 0; i < lines.length; i++) { return "Marker found after processing: " + i + " characters.";
let row = lines[i].trim();
for (let j = 0; j < row.length; j++) {
if (i <= 0 || i >= lines.length - 1 || j <= 0 || j >= row.length - 1) { // Edge node
//console.debug("Edge node found.");
visibleTreeCount++;
continue;
} }
let height = parseInt(lines[i].charAt(j)); marker = marker.substring(1, distinctCharacters) + assignment[i];
if (!walkRowVisible(height, j, row) && !walkColumnVisible(height, i, j, lines)) {
console.log("Found invisible tree, height:" + height + ", i:" + i + ", j:" + j);
visibleTreeCount--;
}
if (getHighestScenicScore) {
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 "No marker found.";
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); * Check if string only contains distinct characters.
} * @param String string the string to check
* @return true, if string contains only distinct characters
function walkRowCount(height, index, row) { */
return walkCount(height, index, row, true, -1); function containsDistinctCharacters(string) {
} console.debug("Check marker: " + string);
return new Set(string).size == string.length;
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);
} }

View File

@@ -9,7 +9,7 @@ body {
textarea { textarea {
display: block; display: block;
} }
#answer { #answer {
color: purple color: purple
} }