Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 7a3dd6bcde |
20
README.md
20
README.md
@@ -1,17 +1,5 @@
|
|||||||
# Advent of Code 2022 - assignment 1
|
# Advent of Code 2022 - Assignment6_javascript
|
||||||
This is the first assignment for Advent of Code 2022. The provided solution uses 3 variables: currentScore, highestCalorieScore, currentElf, highestElf.
|
This repository contains answers to the assignments of Advent of Coding 2022
|
||||||
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.
|
|
||||||
|
|
||||||
## 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://stackoverflow.com/questions/28207610/checking-if-the-characters-in-a-string-are-all-unique
|
||||||
|
|
||||||
## references
|
|
||||||
- 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
|
|
||||||
@@ -8,9 +8,9 @@
|
|||||||
<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">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>
|
||||||
|
|||||||
68
script.js
68
script.js
@@ -1,16 +1,16 @@
|
|||||||
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';
|
||||||
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 +20,39 @@ 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 distinctCharacters = (document.getElementById(ALGORITHM_CHECKBOX).checked)? MESSAGE_DISTINCT_CHARACTERS: MARKER_DISTINCT_CHARACTERS;
|
||||||
let answer = algorithm(assignment, (top3)? TOP_N: 1);
|
let answer = algorithm(assignment, distinctCharacters);
|
||||||
|
|
||||||
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, distinctCharacters) {
|
||||||
let currentCalorieScore = 0;
|
let signal = assignment.trim();
|
||||||
let highestCalorieScores = Array.from({length: topn}, () => (0));
|
let marker = assignment.substring(0,distinctCharacters);
|
||||||
let currentElf = 1;
|
|
||||||
|
|
||||||
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
for(let i=distinctCharacters; i<signal.length; i++) {
|
||||||
console.info("Linecount:" + lines.length);
|
if(containsDistinctCharacters(marker)) {
|
||||||
|
console.debug("Found marker after:" + i + " characters");
|
||||||
|
return "Marker found after processing: " + i + " characters.";
|
||||||
|
}
|
||||||
|
|
||||||
for(var i = 0; i < lines.length; i++){
|
marker = marker.substring(1, distinctCharacters) + assignment[i];
|
||||||
let line = lines[i];
|
|
||||||
|
|
||||||
if(!line && currentCalorieScore > 0) { // Check for empty line and skip subsequent emptylines
|
|
||||||
console.debug("Calculated score for elf " + currentElf + ": " + currentCalorieScore);
|
|
||||||
|
|
||||||
for(let j=0; j < highestCalorieScores.length; j++) {
|
|
||||||
if(currentCalorieScore > highestCalorieScores[j]) {
|
|
||||||
console.debug("New highest #"+(j+1) + " elf found, with score:" + currentCalorieScore);
|
|
||||||
let oldHighestCalorieScore = highestCalorieScores[j];
|
|
||||||
highestCalorieScores[j] = currentCalorieScore;
|
|
||||||
currentCalorieScore = oldHighestCalorieScore;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset values
|
|
||||||
currentCalorieScore = 0;
|
|
||||||
currentElf++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let calorieScore = parseInt(line);
|
|
||||||
currentCalorieScore += calorieScore;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.debug(highestCalorieScores);
|
return "No marker found.";
|
||||||
return "The highest of sum of calorie amount: " + highestCalorieScores.reduce(function(a, b) { return a + b; }, 0);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if string only contains distinct characters.
|
||||||
|
* @param String string the string to check
|
||||||
|
* @return true, if string contains only distinct characters
|
||||||
|
*/
|
||||||
|
function containsDistinctCharacters(string) {
|
||||||
|
console.debug("Check marker: " + string);
|
||||||
|
return new Set(string).size == string.length;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user