Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 63de472207 |
10
README.md
10
README.md
@@ -1,5 +1,9 @@
|
|||||||
# Advent of Code 2022 - Assignment6_javascript
|
# Advent of Code 2022 - Assignment 4 - javascript
|
||||||
This repository contains answers to the assignments of Advent of Coding 2022
|
## Description
|
||||||
|
The problem for assignment4 deals with overlaps and string parsing. The provided solution for this argument uses substring to parse ranges from file. Furthermore it has a checkbox to trigger whether ranges should fully overlap.
|
||||||
|
|
||||||
## References
|
## References
|
||||||
- https://stackoverflow.com/questions/28207610/checking-if-the-characters-in-a-string-are-all-unique
|
- https://www.w3schools.com/jsref/jsref_indexof.asp
|
||||||
|
- https://www.geeksforgeeks.org/convert-a-string-to-an-integer-in-javascript/#:~:text=In%20JavaScript%20parseInt()%20function,argument%20of%20parseInt()%20function.
|
||||||
|
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
|
||||||
|
- https://medium.com/@raihan_tazdid/overlapping-numbers-in-ranges-5d0f2efc294e
|
||||||
@@ -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">Search for message marker?</label>
|
<label for="algorithm">Full overlap</label>
|
||||||
|
|
||||||
<p>Answer:</p>
|
<p>Answer:</p>
|
||||||
<div id="answer">Provide input first</div>
|
<div id="answer">Provide input first</div>
|
||||||
|
|||||||
86
script.js
86
script.js
@@ -1,9 +1,10 @@
|
|||||||
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 ERROR_MESSAGE_INVALID_RANGE = "Invalid range, ";
|
||||||
const MESSAGE_DISTINCT_CHARACTERS = 14;
|
const COMMA_CHARACTER = ',';
|
||||||
const NEWLINE_CHARACTER = '\n';
|
const NEWLINE_CHARACTER = '\n';
|
||||||
|
const RANGE_CHARACTER = '-';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main function
|
* Main function
|
||||||
@@ -20,8 +21,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 distinctCharacters = (document.getElementById(ALGORITHM_CHECKBOX).checked)? MESSAGE_DISTINCT_CHARACTERS: MARKER_DISTINCT_CHARACTERS;
|
let fullOverlap = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
||||||
let answer = algorithm(assignment, distinctCharacters);
|
let answer = algorithm(assignment, fullOverlap);
|
||||||
|
|
||||||
document.getElementById(ANSWER).innerText = answer;
|
document.getElementById(ANSWER).innerText = answer;
|
||||||
}
|
}
|
||||||
@@ -29,30 +30,73 @@ 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 fullOverlap if input needs to fully overlap.
|
||||||
* @return string the answer
|
* @return string the answer
|
||||||
*/
|
*/
|
||||||
function algorithm(assignment, distinctCharacters) {
|
function algorithm(assignment, fullOverlap) {
|
||||||
let signal = assignment.trim();
|
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||||
let marker = assignment.substring(0,distinctCharacters);
|
console.info("Linecount:" + lines.length);
|
||||||
|
|
||||||
for(let i=distinctCharacters; i<signal.length; i++) {
|
let containedPairs = 0;
|
||||||
if(containsDistinctCharacters(marker)) {
|
for(let i=0; i<lines.length; i++) {
|
||||||
console.debug("Found marker after:" + i + " characters");
|
let error = false;
|
||||||
return "Marker found after processing: " + i + " characters.";
|
let ranges = lines[i].trim();
|
||||||
|
// Search for separation charactes -,-
|
||||||
|
let range1CharacterIndex = ranges.indexOf(RANGE_CHARACTER);
|
||||||
|
let range1EndCharacterIndex = ranges.indexOf(COMMA_CHARACTER);
|
||||||
|
let range2CharacterIndex = ranges.indexOf(RANGE_CHARACTER, range1CharacterIndex+1);
|
||||||
|
|
||||||
|
// Get range substrings
|
||||||
|
let range1LowerString = ranges.substring(0,range1CharacterIndex);
|
||||||
|
let range1HigherString = ranges.substring(range1CharacterIndex+1, range1EndCharacterIndex);
|
||||||
|
let range2LowerString = ranges.substring(range1EndCharacterIndex+1, range2CharacterIndex);
|
||||||
|
let range2HigherString = ranges.substring(range2CharacterIndex+1);
|
||||||
|
console.debug("Parsed values: " + range1LowerString + "-" + range1HigherString + "," + range2LowerString + "-" + range2HigherString);
|
||||||
|
|
||||||
|
// Parse to int
|
||||||
|
let range1Lower = parseInt(range1LowerString);
|
||||||
|
let range1Higher = parseInt(range1HigherString);
|
||||||
|
let range2Lower = parseInt(range2LowerString);
|
||||||
|
let range2Higher = parseInt(range2HigherString);
|
||||||
|
|
||||||
|
// Validation
|
||||||
|
if(range1Lower > range1Higher) {
|
||||||
|
console.error(ERROR_MESSAGE_INVALID_RANGE + range1Lower + "-" + range1Higher + " on line:" + i + " for range1");
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
if(range2Lower > range2Higher) {
|
||||||
|
console.error(ERROR_MESSAGE_INVALID_RANGE + range2Lower + "-" + range2Higher + " on line:" + i + " for range2");
|
||||||
|
error = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
marker = marker.substring(1, distinctCharacters) + assignment[i];
|
if(error) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return "No marker found.";
|
// Check for overlap
|
||||||
|
if(fullOverlap && doesRangeFullyOverlap(range1Lower, range1Higher, range2Lower, range2Higher)
|
||||||
|
|| !fullOverlap && doesRangeOverlap(range1Lower, range1Higher, range2Lower, range2Higher)) {
|
||||||
|
console.debug("Found overlapping range:" + range1Lower + "-" + range1Higher + "," + range2Lower + "-" + range2Higher);
|
||||||
|
containedPairs++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Fully overlapping timeslot counts: " + containedPairs;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function doesRangeFullyOverlap(lower, higher, lower2, higher2) {
|
||||||
* Check if string only contains distinct characters.
|
console.debug("Compare range: " + lower + "-" + higher + " with " + lower2 + "-" + higher2);
|
||||||
* @param String string the string to check
|
if(lower > higher || lower2 > higher2) {
|
||||||
* @return true, if string contains only distinct characters
|
return false;
|
||||||
*/
|
}
|
||||||
function containsDistinctCharacters(string) {
|
return ((lower <= lower2 && higher >= higher2) || (lower2 <= lower && higher2 >= higher));
|
||||||
console.debug("Check marker: " + string);
|
}
|
||||||
return new Set(string).size == string.length;
|
|
||||||
|
function doesRangeOverlap(lower, higher, lower2, higher2) {
|
||||||
|
console.debug("Compare range: " + lower + "-" + higher + " with " + lower2 + "-" + higher2);
|
||||||
|
if(lower > higher || lower2 > higher2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return higher2 >= lower && lower2 <= higher;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user