diff --git a/README.md b/README.md
index da57339..dd25081 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,9 @@
-This repository contains answers to the assignments of Advent of Coding 2022
+# Advent of Code 2022 - Assignment 4 - javascript
+## 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
+- 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
\ No newline at end of file
diff --git a/index.html b/index.html
index 5cf1ba4..babe486 100644
--- a/index.html
+++ b/index.html
@@ -8,6 +8,8 @@
Assignment:
+
+ Full overlap
Answer:
Provide input first
diff --git a/script.js b/script.js
index a6e2848..d2f6177 100644
--- a/script.js
+++ b/script.js
@@ -1,12 +1,17 @@
const ASSIGNMENT = 'assignment';
const ANSWER = 'answer';
+const ALGORITHM_CHECKBOX = 'algorithm';
+const ERROR_MESSAGE_INVALID_RANGE = "Invalid range, ";
+const COMMA_CHARACTER = ',';
const NEWLINE_CHARACTER = '\n';
+const RANGE_CHARACTER = '-';
/**
* Main function
*/
window.onload = function() {
document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer);
+ document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", calculateAnswer);
}
/**
@@ -15,7 +20,9 @@ window.onload = function() {
*/
function calculateAnswer(event) {
console.info("Calculating answer for input...");
- let answer = algorithm(event.target.value);
+ let assignment = document.getElementById(ASSIGNMENT).value;
+ let fullOverlap = document.getElementById(ALGORITHM_CHECKBOX).checked;
+ let answer = algorithm(assignment, fullOverlap);
document.getElementById(ANSWER).innerText = answer;
}
@@ -23,11 +30,73 @@ function calculateAnswer(event) {
/**
* Calculate the answer to assignment.
* @param assignment the input from the assignment.
+ * @param fullOverlap if input needs to fully overlap.
* @return string the answer
*/
-function algorithm(assignment) {
+function algorithm(assignment, fullOverlap) {
let lines = assignment.trim().split(NEWLINE_CHARACTER);
console.info("Linecount:" + lines.length);
- // TODO: implement assignment
+ let containedPairs = 0;
+ for(let i=0; i 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;
+ }
+
+ if(error) {
+ continue;
+ }
+
+ // 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) {
+ console.debug("Compare range: " + lower + "-" + higher + " with " + lower2 + "-" + higher2);
+ if(lower > higher || lower2 > higher2) {
+ return false;
+ }
+ return ((lower <= lower2 && higher >= higher2) || (lower2 <= lower && higher2 >= higher));
+}
+
+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;
}
\ No newline at end of file
diff --git a/style.css b/style.css
index f39a6bd..861cba6 100644
--- a/style.css
+++ b/style.css
@@ -6,6 +6,10 @@ body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
+ textarea {
+ display: block;
+ }
+
#answer {
color: purple
}