Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 3f3daab726 |
28
README.md
28
README.md
@@ -1,13 +1,17 @@
|
|||||||
# Advent of Code 2022 - Assignment5 javascript
|
# Advent of Code 2022 - assignment 1
|
||||||
This repository contains answers to the assignments of Advent of Coding 2022
|
This is the first assignment for Advent of Code 2022. The provided solution uses 3 variables: currentScore, highestCalorieScore, currentElf, highestElf.
|
||||||
|
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.
|
||||||
|
|
||||||
## References
|
## Solution description
|
||||||
- https://www.w3schools.com/jsref/jsref_includes.asp
|
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://stackoverflow.com/questions/10261986/how-to-detect-string-which-contains-only-spaces
|
|
||||||
- https://www.w3schools.com/jsref/jsref_indexof.asp
|
## references
|
||||||
- https://stackoverflow.com/questions/8073673/how-can-i-add-new-array-elements-at-the-beginning-of-an-array-in-javascript
|
- https://adventofcode.com/2022/day/1
|
||||||
- https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript
|
- https://stackoverflow.com/questions/17595361/how-to-read-text-line-by-line-in-a-html-text-area
|
||||||
- https://www.w3schools.com/jsref/jsref_ceil.asp
|
- https://bobbyhadz.com/blog/javascript-get-value-of-textarea
|
||||||
- https://stackoverflow.com/questions/30561056/console-log-a-multi-dimensional-array
|
- https://stackoverflow.com/questions/154059/how-do-i-check-for-an-empty-undefined-null-string-in-javascript
|
||||||
- https://teamtreehouse.com/community/removing-more-than-1-element-using-pop-and-shift-method
|
- 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/14723848/push-multiple-elements-to-array
|
- 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
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
<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">TOP 3</label>
|
||||||
|
|
||||||
<p>Answer:</p>
|
<p>Answer:</p>
|
||||||
<div id="answer">Provide input first</div>
|
<div id="answer">Provide input first</div>
|
||||||
|
|||||||
161
script.js
161
script.js
@@ -1,11 +1,9 @@
|
|||||||
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';
|
||||||
|
const TOP_1 = 1;
|
||||||
|
const TOP_N = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function
|
* Main function
|
||||||
@@ -22,146 +20,51 @@ 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 top3 = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
||||||
let answer = algorithm(assignment, allAtOnce);
|
let answer = algorithm(assignment, (top3)? TOP_N: 1);
|
||||||
|
|
||||||
document.getElementById(ANSWER).innerText = answer;
|
document.getElementById(ANSWER).innerText = answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the answer to assignment.
|
* Calculate the answer to assignment1 with 2 variables, count and highest.
|
||||||
* @param assignment the input from the assignment.
|
* @param assignment the input from the assignment.
|
||||||
* @param allAtOnce should all containers be moved at once.
|
* @param topn the
|
||||||
* @return string the answer
|
* @return string the elf with the highest calory count
|
||||||
*/
|
*/
|
||||||
function algorithm(assignment, allAtOnce) {
|
function algorithm(assignment, topn) {
|
||||||
let lines = assignment.split(NEWLINE_CHARACTER);
|
let currentCalorieScore = 0;
|
||||||
let containerPlan = Array.from({length:(lines[0].length+1)/4}, () => [])
|
let highestCalorieScores = Array.from({length: topn}, () => (0));
|
||||||
|
let currentElf = 1;
|
||||||
|
|
||||||
|
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||||
console.info("Linecount:" + lines.length);
|
console.info("Linecount:" + lines.length);
|
||||||
|
|
||||||
let parsec = true;
|
for(var i = 0; i < lines.length; i++){
|
||||||
for(let i=0; i<lines.length; i++) {
|
let line = lines[i];
|
||||||
if(parsec) {
|
|
||||||
|
|
||||||
// Parse containers
|
if(!line && currentCalorieScore > 0) { // Check for empty line and skip subsequent emptylines
|
||||||
let containerLine = lines[i];
|
console.debug("Calculated score for elf " + currentElf + ": " + currentCalorieScore);
|
||||||
|
|
||||||
// Skip numbers
|
for(let j=0; j < highestCalorieScores.length; j++) {
|
||||||
if(containerLine[1] == '1') {
|
if(currentCalorieScore > highestCalorieScores[j]) {
|
||||||
parsec = false;
|
console.debug("New highest #"+(j+1) + " elf found, with score:" + currentCalorieScore);
|
||||||
i+=1;
|
let oldHighestCalorieScore = highestCalorieScores[j];
|
||||||
console.table(containerPlan);
|
highestCalorieScores[j] = currentCalorieScore;
|
||||||
continue;
|
currentCalorieScore = oldHighestCalorieScore;
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
// Reset values
|
||||||
if(instruction == -1) {
|
currentCalorieScore = 0;
|
||||||
// invalid instructionline
|
currentElf++;
|
||||||
// TODO: error
|
continue;
|
||||||
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 calorieScore = parseInt(line);
|
||||||
|
currentCalorieScore += calorieScore;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result="";
|
console.debug(highestCalorieScores);
|
||||||
for(let k=0; k<containerPlan.length; k++) {
|
return "The highest of sum of calorie amount: " + highestCalorieScores.reduce(function(a, b) { return a + b; }, 0);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user