From 3139c49f5059e472c84b1ef06c53242db3d49aec Mon Sep 17 00:00:00 2001 From: paulusgaming Date: Sun, 4 Dec 2022 23:30:29 +0100 Subject: [PATCH] Solution for assignment3. --- README.md | 13 +++++- index.html | 2 + script.js | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++-- style.css | 4 ++ 4 files changed, 141 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index da57339..66bda1f 100644 --- a/README.md +++ b/README.md @@ -1 +1,12 @@ -This repository contains answers to the assignments of Advent of Coding 2022 +# Advent of Code 2022 - Assignment3 javascript solution +This problem is all about searching for duplicates in strings. The solution is implemented using slice to split the string in half. +The priority is retrieved using the ASCII charcode character for convienence. How to retrieve inputs differs between part1 and 2, therefore a checkbox is provided to toggle between different parsing methods. + +## References +- https://stackoverflow.com/questions/94037/convert-character-to-ascii-code-in-javascript +- https://stackoverflow.com/questions/20474257/split-string-into-two-parts +- https://www.folkstalk.com/2022/10/find-common-characters-in-two-strings-javascript-with-code-examples.html +- https://stackoverflow.com/questions/351409/how-to-append-something-to-an-array +- https://stackoverflow.com/questions/9862761/how-to-check-if-character-is-a-letter-in-javascript +- https://stackoverflow.com/questions/1966476/how-can-i-process-each-letter-of-text-using-javascript +- https://www.w3schools.com/js/js_arrays.asp \ No newline at end of file diff --git a/index.html b/index.html index 5cf1ba4..c56ec6a 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,8 @@

Assignment:

+ +

Answer:

Provide input first
diff --git a/script.js b/script.js index a6e2848..887ec4e 100644 --- a/script.js +++ b/script.js @@ -1,5 +1,10 @@ const ASSIGNMENT = 'assignment'; const ANSWER = 'answer'; +const ALGORITHM_CHECKBOX = 'algorithm'; +const CHARCODE_START=64; +const CHARCODE_UPPER_CORRECTION=6; +const ERROR_MESSAGE_INVALID_INPUT_SIZE = "Invalid input size, should be higher than 2 for: "; +const ERROR_MESSAGE_INVALID_ITEM_CHARACTER = "Invalid item character found: "; const NEWLINE_CHARACTER = '\n'; /** @@ -7,6 +12,7 @@ const NEWLINE_CHARACTER = '\n'; */ window.onload = function() { document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer); + document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", calculateAnswer); } /** @@ -15,7 +21,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 byCompartiments = document.getElementById(ALGORITHM_CHECKBOX).checked; + let answer = algorithm(assignment, byCompartiments); document.getElementById(ANSWER).innerText = answer; } @@ -25,9 +33,121 @@ function calculateAnswer(event) { * @param assignment the input from the assignment. * @return string the answer */ -function algorithm(assignment) { +function algorithm(assignment, byCompartiments) { + let priorityScoreSum = 0; let lines = assignment.trim().split(NEWLINE_CHARACTER); console.info("Linecount:" + lines.length); - // TODO: implement assignment + let increment = (byCompartiments)? 1:3; + for(let i=0; i