Initial assignment1 solution.
This commit is contained in:
16
README.md
16
README.md
@@ -1 +1,15 @@
|
||||
This repository contains answers to the assignments of Advent of Coding 2022
|
||||
# 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²) since it loops through every line exactly once and the iterates for recalculating highscore.
|
||||
|
||||
## 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
|
||||
- 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
|
||||
16
index.html
Normal file
16
index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<p>Assignment:</p>
|
||||
<textarea rows="15" cols="50" id="assignment"></textarea>
|
||||
|
||||
<p>Answer:</p>
|
||||
<div id="answer">Provide input first</div>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
63
script.js
Normal file
63
script.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const ASSIGNMENT = 'assignment';
|
||||
const ANSWER = 'answer';
|
||||
const NEWLINE_CHARACTER = '\n';
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
window.onload = function() {
|
||||
document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener function for input in assignment field.
|
||||
* @param event the onInput event
|
||||
*/
|
||||
function calculateAnswer(event) {
|
||||
console.info("Calculating answer for input...");
|
||||
let answer = algorithm(event.target.value);
|
||||
|
||||
document.getElementById(ANSWER).innerText = answer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the answer to assignment1 with 2 variables, count and highest.
|
||||
* @param assignment the input from the assignment.
|
||||
* @return string the elf with the highest calory count
|
||||
*/
|
||||
function algorithm(assignment) {
|
||||
let currentCalorieScore = 0;
|
||||
let highestCalorieScores = [0,0,0];
|
||||
let currentElf = 1;
|
||||
|
||||
let lines = assignment.trim().split(NEWLINE_CHARACTER);
|
||||
console.info("Linecount:" + lines.length);
|
||||
|
||||
for(var i = 0; i < lines.length; 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 "The highest of sum of calorie amount: " + highestCalorieScores.reduce(function(a, b) { return a + b; }, 0);
|
||||
}
|
||||
15
style.css
Normal file
15
style.css
Normal file
@@ -0,0 +1,15 @@
|
||||
body {
|
||||
background: transparent; /* Make it white if you need */
|
||||
color: #fc8224;
|
||||
padding: 0 24px;
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
||||
}
|
||||
|
||||
#answer {
|
||||
color: purple
|
||||
}
|
||||
|
||||
p {
|
||||
margin: 0px
|
||||
}
|
||||
Reference in New Issue
Block a user