Compare commits
1 Commits
assignment
...
assignment
| Author | SHA1 | Date | |
|---|---|---|---|
| 63de472207 |
22
README.md
22
README.md
@@ -1,17 +1,9 @@
|
||||
# Advent of Code 2022 - assignment 1
|
||||
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.
|
||||
# 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.
|
||||
|
||||
## Solution description
|
||||
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.
|
||||
|
||||
## 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
|
||||
## 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://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
|
||||
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
|
||||
- https://medium.com/@raihan_tazdid/overlapping-numbers-in-ranges-5d0f2efc294e
|
||||
@@ -9,7 +9,7 @@
|
||||
<p>Assignment:</p>
|
||||
<textarea rows="15" cols="50" id="assignment"></textarea>
|
||||
<input type="checkbox" id="algorithm"/>
|
||||
<label for="algorithm">TOP 3</label>
|
||||
<label for="algorithm">Full overlap</label>
|
||||
|
||||
<p>Answer:</p>
|
||||
<div id="answer">Provide input first</div>
|
||||
|
||||
96
script.js
96
script.js
@@ -1,9 +1,10 @@
|
||||
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 TOP_1 = 1;
|
||||
const TOP_N = 3;
|
||||
const RANGE_CHARACTER = '-';
|
||||
|
||||
/**
|
||||
* Main function
|
||||
@@ -20,51 +21,82 @@ window.onload = function() {
|
||||
function calculateAnswer(event) {
|
||||
console.info("Calculating answer for input...");
|
||||
let assignment = document.getElementById(ASSIGNMENT).value;
|
||||
let top3 = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
||||
let answer = algorithm(assignment, (top3)? TOP_N: 1);
|
||||
let fullOverlap = document.getElementById(ALGORITHM_CHECKBOX).checked;
|
||||
let answer = algorithm(assignment, fullOverlap);
|
||||
|
||||
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 topn the
|
||||
* @return string the elf with the highest calory count
|
||||
* @param fullOverlap if input needs to fully overlap.
|
||||
* @return string the answer
|
||||
*/
|
||||
function algorithm(assignment, topn) {
|
||||
let currentCalorieScore = 0;
|
||||
let highestCalorieScores = Array.from({length: topn}, () => (0));
|
||||
let currentElf = 1;
|
||||
|
||||
function algorithm(assignment, fullOverlap) {
|
||||
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||
console.info("Linecount:" + lines.length);
|
||||
|
||||
for(var i = 0; i < lines.length; i++){
|
||||
let line = lines[i];
|
||||
let containedPairs = 0;
|
||||
for(let i=0; i<lines.length; i++) {
|
||||
let error = false;
|
||||
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);
|
||||
|
||||
if(!line && currentCalorieScore > 0) { // Check for empty line and skip subsequent emptylines
|
||||
console.debug("Calculated score for elf " + currentElf + ": " + currentCalorieScore);
|
||||
// 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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
// Parse to int
|
||||
let range1Lower = parseInt(range1LowerString);
|
||||
let range1Higher = parseInt(range1HigherString);
|
||||
let range2Lower = parseInt(range2LowerString);
|
||||
let range2Higher = parseInt(range2HigherString);
|
||||
|
||||
// Reset values
|
||||
currentCalorieScore = 0;
|
||||
currentElf++;
|
||||
// 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;
|
||||
}
|
||||
|
||||
if(error) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let calorieScore = parseInt(line);
|
||||
currentCalorieScore += calorieScore;
|
||||
|
||||
// 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++;
|
||||
}
|
||||
}
|
||||
|
||||
console.debug(highestCalorieScores);
|
||||
return "The highest of sum of calorie amount: " + highestCalorieScores.reduce(function(a, b) { return a + b; }, 0);
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user