Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 28597a3f26 |
17
README.md
17
README.md
@@ -1,13 +1,8 @@
|
|||||||
# Advent of Code 2022 - Assignment5 javascript
|
# Advent of Code 2022 - Assignment8 javascript
|
||||||
This repository contains answers to the assignments of Advent of Coding 2022
|
## Description
|
||||||
|
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_includes.asp
|
- https://www.w3schools.com/jsref/jsref_charat.asp
|
||||||
- https://stackoverflow.com/questions/10261986/how-to-detect-string-which-contains-only-spaces
|
- https://stackabuse.com/javascript-check-if-variable-is-a-number/
|
||||||
- https://www.w3schools.com/jsref/jsref_indexof.asp
|
- https://www.w3schools.com/jsref/jsref_abs.asp
|
||||||
- https://stackoverflow.com/questions/8073673/how-can-i-add-new-array-elements-at-the-beginning-of-an-array-in-javascript
|
|
||||||
- https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript
|
|
||||||
- https://www.w3schools.com/jsref/jsref_ceil.asp
|
|
||||||
- https://stackoverflow.com/questions/30561056/console-log-a-multi-dimensional-array
|
|
||||||
- https://teamtreehouse.com/community/removing-more-than-1-element-using-pop-and-shift-method
|
|
||||||
- https://stackoverflow.com/questions/14723848/push-multiple-elements-to-array
|
|
||||||
@@ -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">All containers at once?</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>
|
||||||
|
|||||||
213
script.js
213
script.js
@@ -1,18 +1,14 @@
|
|||||||
const ASSIGNMENT = 'assignment';
|
const ASSIGNMENT = 'assignment';
|
||||||
const ANSWER = 'answer';
|
const ANSWER = 'answer';
|
||||||
const ALGORITHM_CHECKBOX = 'algorithm';
|
const ALGORITHM_CHECKBOX = 'algorithm';
|
||||||
const CONTAINER_NULL_CHARACTER=0;
|
|
||||||
const INSTRUCTION_FROM='from';
|
|
||||||
const INSTRUCTION_MOVE='move';
|
|
||||||
const INSTRUCTION_TO='to';
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,8 +18,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 allAtOnce = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
let getHighestScenicScore = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
||||||
let answer = algorithm(assignment, allAtOnce);
|
let answer = algorithm(assignment, getHighestScenicScore);
|
||||||
|
|
||||||
document.getElementById(ANSWER).innerText = answer;
|
document.getElementById(ANSWER).innerText = answer;
|
||||||
}
|
}
|
||||||
@@ -31,137 +27,108 @@ function calculateAnswer(event) {
|
|||||||
/**
|
/**
|
||||||
* Calculate the answer to assignment.
|
* Calculate the answer to assignment.
|
||||||
* @param assignment the input from the assignment.
|
* @param assignment the input from the assignment.
|
||||||
* @param allAtOnce should all containers be moved at once.
|
|
||||||
* @return string the answer
|
* @return string the answer
|
||||||
*/
|
*/
|
||||||
function algorithm(assignment, allAtOnce) {
|
function algorithm(assignment, getHighestScenicScore) {
|
||||||
let lines = assignment.split(NEWLINE_CHARACTER);
|
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||||
let containerPlan = Array.from({length:(lines[0].length+1)/4}, () => [])
|
console.info("Linecount:" + lines.length);
|
||||||
console.info("Linecount:" + lines.length);
|
|
||||||
|
|
||||||
let parsec = true;
|
let visibleTreeCount = 0;
|
||||||
for(let i=0; i<lines.length; i++) {
|
let highestScenicScore = 0;
|
||||||
if(parsec) {
|
|
||||||
|
|
||||||
// Parse containers
|
for (let i = 0; i < lines.length; i++) {
|
||||||
let containerLine = lines[i];
|
let row = lines[i].trim();
|
||||||
|
|
||||||
// Skip numbers
|
for (let j = 0; j < row.length; j++) {
|
||||||
if(containerLine[1] == '1') {
|
if (i <= 0 || i >= lines.length - 1 || j <= 0 || j >= row.length - 1) { // Edge node
|
||||||
parsec = false;
|
//console.debug("Edge node found.");
|
||||||
i+=1;
|
visibleTreeCount++;
|
||||||
console.table(containerPlan);
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let containers = parseContainers(containerLine);
|
let height = parseInt(lines[i].charAt(j));
|
||||||
if(containers == -1) {
|
if (!walkRowVisible(height, j, row) && !walkColumnVisible(height, i, j, lines)) {
|
||||||
// invalid containerline
|
console.log("Found invisible tree, height:" + height + ", i:" + i + ", j:" + j);
|
||||||
// TODO: error
|
visibleTreeCount--;
|
||||||
console.error("Container parse failure.");
|
}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add containers
|
if (getHighestScenicScore) {
|
||||||
//console.debug(containers);
|
let scenicScore = walkRowCount(height, j, row) * walkColumnCount(height, i, j, lines);
|
||||||
for(let j=0; j<containers.length; j++) {
|
if (scenicScore > highestScenicScore) {
|
||||||
let container = containers[j];
|
console.debug("Found new highest scenic score: " + scenicScore);
|
||||||
//console.debug(container);
|
highestScenicScore = scenicScore;
|
||||||
if(container != CONTAINER_NULL_CHARACTER) {
|
|
||||||
//console.debug(j);
|
|
||||||
//console.debug(containerPlan);
|
|
||||||
containerPlan[j].push(container);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//console.table(containerPlan);
|
|
||||||
} else {
|
|
||||||
// Parse instructions
|
|
||||||
let instructionLine = lines[i];
|
|
||||||
let instruction = parseInstruction(instructionLine);
|
|
||||||
|
|
||||||
console.debug(instruction);
|
|
||||||
if(instruction == -1) {
|
|
||||||
// invalid instructionline
|
|
||||||
// TODO: error
|
|
||||||
console.error("Instruction parse failure.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute instruction
|
|
||||||
let movecount=0;
|
|
||||||
if(allAtOnce) {
|
|
||||||
while(instruction.move > 0) {
|
|
||||||
let container = containerPlan[instruction.from].shift();
|
|
||||||
containerPlan[instruction.to].unshift(container);
|
|
||||||
instruction.move--;
|
|
||||||
movecount++;
|
|
||||||
}
|
|
||||||
console.debug("moved:" + movecount);
|
|
||||||
} else {
|
|
||||||
let splicedContainers = containerPlan[instruction.from].splice(0, instruction.move);
|
|
||||||
containerPlan[instruction.to].unshift(...splicedContainers);
|
|
||||||
}
|
|
||||||
|
|
||||||
//console.table(containerPlan);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let result="";
|
// Tree is on edge.
|
||||||
for(let k=0; k<containerPlan.length; k++) {
|
visibleTreeCount++;
|
||||||
result += containerPlan[k][0];
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return "Top containers on the stack are: " + result;
|
if (getHighestScenicScore) {
|
||||||
|
return "Highest scenic score: " + highestScenicScore;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Amount of visible trees in the grid: " + visibleTreeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseInstruction(instructionLine) {
|
function walkColumnCount(height, index, rowIndex, column) {
|
||||||
let instruction={
|
return walkCount(height, index, column, false, rowIndex);
|
||||||
move:0,
|
|
||||||
from:0,
|
|
||||||
to:0,
|
|
||||||
}
|
|
||||||
|
|
||||||
instructionLine.replace(/\s/g, ''); // Remove whitespace
|
|
||||||
let fromIndex = instructionLine.indexOf(INSTRUCTION_FROM);
|
|
||||||
let moveIndex = instructionLine.indexOf(INSTRUCTION_MOVE);
|
|
||||||
let toIndex = instructionLine.indexOf(INSTRUCTION_TO);
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
if(fromIndex < 0 || moveIndex < 0 || toIndex < 0) {
|
|
||||||
// Error
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO validate check for numbers after indices.
|
|
||||||
|
|
||||||
// Parse
|
|
||||||
instruction.move = parseInt(instructionLine.substring(moveIndex + INSTRUCTION_MOVE.length + 1, fromIndex));
|
|
||||||
instruction.from = parseInt(instructionLine.substring(fromIndex + INSTRUCTION_FROM.length + 1, toIndex)) -1;
|
|
||||||
instruction.to = parseInt(instructionLine.substring(toIndex + INSTRUCTION_TO.length + 1)) -1;
|
|
||||||
|
|
||||||
return instruction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseContainers(containerLine) {
|
function walkRowCount(height, index, row) {
|
||||||
let containerLineLength = (containerLine.length+1);
|
return walkCount(height, index, row, true, -1);
|
||||||
//console.debug("Parse ContainerLine, length: " + containerLineLength);
|
}
|
||||||
|
|
||||||
if(!containerLine.includes('[') || !containerLine.includes(']') || containerLineLength % 4 != 0) {
|
function walkColumnVisible(height, index, rowIndex, column) {
|
||||||
return -1;
|
return walkVisible(height, index, column, false, rowIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
let containers = [];
|
function walkRowVisible(height, index, row) {
|
||||||
for(let i=0; i<containerLineLength; i+=4) {
|
return walkVisible(height, index, row, true, -1);
|
||||||
let containerLineSubString = containerLine.substring(i, i+4).trim();
|
}
|
||||||
let character = containerLineSubString[1];
|
|
||||||
if(containerLineSubString[0] != '[' || containerLineSubString[2] != ']') {
|
|
||||||
// Warning invalid container format
|
|
||||||
// TODO: warning
|
|
||||||
}
|
|
||||||
|
|
||||||
containers.push((!character)? CONTAINER_NULL_CHARACTER: character);
|
function walkCount(height, index, line, isRow, rowIndex) {
|
||||||
}
|
let visibleLeftCount = __walkHelper(height, index, line, isRow, -1, rowIndex, 0);
|
||||||
//console.debug(containers);
|
let visibleRightCount = __walkHelper(height, index, line, isRow, 1, rowIndex, 0);
|
||||||
|
|
||||||
return containers;
|
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);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user