Assignment on my birthday

This commit is contained in:
2022-12-06 17:26:38 +01:00
parent 8d25208c83
commit 7a3dd6bcde
4 changed files with 42 additions and 7 deletions

View File

@@ -1 +1,5 @@
# Advent of Code 2022 - Assignment6_javascript
This repository contains answers to the assignments of Advent of Coding 2022 This repository contains answers to the assignments of Advent of Coding 2022
## References
- https://stackoverflow.com/questions/28207610/checking-if-the-characters-in-a-string-are-all-unique

View File

@@ -8,6 +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" />
<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>

View File

@@ -1,12 +1,16 @@
const ASSIGNMENT = 'assignment'; const ASSIGNMENT = 'assignment';
const ANSWER = 'answer'; const ANSWER = 'answer';
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);
} }
/** /**
@@ -15,7 +19,9 @@ window.onload = function() {
*/ */
function calculateAnswer(event) { function calculateAnswer(event) {
console.info("Calculating answer for input..."); console.info("Calculating answer for input...");
let answer = algorithm(event.target.value); let assignment = document.getElementById(ASSIGNMENT).value;
let distinctCharacters = (document.getElementById(ALGORITHM_CHECKBOX).checked)? MESSAGE_DISTINCT_CHARACTERS: MARKER_DISTINCT_CHARACTERS;
let answer = algorithm(assignment, distinctCharacters);
document.getElementById(ANSWER).innerText = answer; document.getElementById(ANSWER).innerText = answer;
} }
@@ -25,9 +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) { 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);
// TODO: implement assignment for(let i=distinctCharacters; i<signal.length; i++) {
if(containsDistinctCharacters(marker)) {
console.debug("Found marker after:" + i + " characters");
return "Marker found after processing: " + i + " characters.";
}
marker = marker.substring(1, distinctCharacters) + assignment[i];
}
return "No marker found.";
}
/**
* 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;
} }

View File

@@ -6,6 +6,10 @@ body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
} }
textarea {
display: block;
}
#answer { #answer {
color: purple color: purple
} }