Day 1: Part 2

This commit is contained in:
Fawkes100 2024-12-08 09:12:26 +01:00
parent 939cf8b74a
commit 911a30b931
4 changed files with 40 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
Aufgabe1/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View File

@ -117,6 +117,25 @@ int solve(std::vector<int>& left, std::vector<int>& right) {
return sum; return sum;
} }
int countOccurenceInList(std::vector<int>& vec, int element) {
int occurences = 0;
for(const auto& i: vec) {
if(i == element) {
occurences++;
}
}
return occurences;
}
int calcSimilarity(std::vector<int>& left, std::vector<int>& right) {
int similarityScore = 0;
for(int i : left) {
int occurencesInRightList = countOccurenceInList(right, i);
similarityScore += i * occurencesInRightList;
}
return similarityScore;
}
int main() { int main() {
std::string inputFile = "../input.txt"; std::string inputFile = "../input.txt";
std::vector<std::string> input = readTextFromFile(inputFile); std::vector<std::string> input = readTextFromFile(inputFile);
@ -124,5 +143,8 @@ int main() {
int result = solve(leftArray, rightArray); int result = solve(leftArray, rightArray);
std::cout << "The result is: " << result << std::endl; std::cout << "The result is: " << result << std::endl;
int similarityScore = calcSimilarity(leftArray, rightArray);
std::cout << "The similarity is " << similarityScore << std::endl;
return 0; return 0;
} }