Files
advent-of-code/script.js
2022-12-06 17:03:21 +01:00

167 lines
5.3 KiB
JavaScript

const ASSIGNMENT = 'assignment';
const ANSWER = 'answer';
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';
/**
* Main function
*/
window.onload = function() {
document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer);
document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", calculateAnswer);
}
/**
* Listener function for input in assignment field.
* @param event the onInput event
*/
function calculateAnswer(event) {
console.info("Calculating answer for input...");
let assignment = document.getElementById(ASSIGNMENT).value;
let allAtOnce = document.getElementById(ALGORITHM_CHECKBOX).checked;
let answer = algorithm(assignment, allAtOnce);
document.getElementById(ANSWER).innerText = answer;
}
/**
* Calculate the answer to assignment.
* @param assignment the input from the assignment.
* @param allAtOnce should all containers be moved at once.
* @return string the answer
*/
function algorithm(assignment, allAtOnce) {
let lines = assignment.split(NEWLINE_CHARACTER);
let containerPlan = Array.from({length:(lines[0].length+1)/4}, () => [])
console.info("Linecount:" + lines.length);
let parsec = true;
for(let i=0; i<lines.length; i++) {
if(parsec) {
// Parse containers
let containerLine = lines[i];
// Skip numbers
if(containerLine[1] == '1') {
parsec = false;
i+=1;
console.table(containerPlan);
continue;
}
let containers = parseContainers(containerLine);
if(containers == -1) {
// invalid containerline
// TODO: error
console.error("Container parse failure.");
continue;
}
// Add containers
//console.debug(containers);
for(let j=0; j<containers.length; j++) {
let container = containers[j];
//console.debug(container);
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="";
for(let k=0; k<containerPlan.length; k++) {
result += containerPlan[k][0];
}
return "Top containers on the stack are: " + result;
}
function parseInstruction(instructionLine) {
let instruction={
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) {
let containerLineLength = (containerLine.length+1);
//console.debug("Parse ContainerLine, length: " + containerLineLength);
if(!containerLine.includes('[') || !containerLine.includes(']') || containerLineLength % 4 != 0) {
return -1;
}
let containers = [];
for(let i=0; i<containerLineLength; i+=4) {
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);
}
//console.debug(containers);
return containers;
}