From c165037fb9ecacec17839c3a409ab37a4f92f070 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 6 Dec 2020 15:27:50 -0500 Subject: [PATCH] Added solution to Day6-2 --- Day6-2.cpp | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Day6-2.cpp diff --git a/Day6-2.cpp b/Day6-2.cpp new file mode 100644 index 0000000..f6db292 --- /dev/null +++ b/Day6-2.cpp @@ -0,0 +1,129 @@ +//Fun/AdventCalendar2020/Day6-2.cpp +//Matthew Ellison +// Created: 12-06-20 +//Modified: 12-06-20 +//The sum of the counts of the questions answered yes in each group + + +#include +#include +#include +#include +#include "Stopwatch.hpp" +#include "Algorithms.hpp" + + +bool isFound(std::vector ary, char ch){ + for(char c : ary){ + if(c == ch){ + return true; + } + } + return false; +} +struct Person{ + std::vector yeses; +}; +struct Group{ + std::vector people; + std::vector getYeses(){ + std::vector yeses; + //Go through every person in people and gather their yes answers into a single vector + for(Person per : people){ + for(char ch : per.yeses){ + //Add them to the vector only if they don't already exist + if(!isFound(yeses, ch)){ + yeses.push_back(ch); + } + } + } + //Return the vector with all the yes answers + return yeses; + } + std::vector getUniversalYeses(){ + std::vector yeses; + //You only need to look at the characters for the first person + for(char ch : people.front().yeses){ + int yesCount = 1; + //Count how many times the character appears in all of the people + for(int cnt = 1;cnt < people.size();++cnt){ + if(isFound(people[cnt].yeses, ch)){ + ++yesCount; + } + } + //If the character was in everyones "yeses" then add it to the array + if(yesCount == people.size()){ + yeses.push_back(ch); + } + } + //Return the array of all univeral yeses + return yeses; + } +}; + + +std::vector getInput(){ + std::ifstream inputFile("inputs/Day6.txt"); + std::vector groups; + + //Go through the entire file + while(!inputFile.eof()){ + Group group; + std::string currentLine; + //Each new line is a new person + std::getline(inputFile, currentLine); + //Go through the file until you find an empty line + //An empty line signifies a new group + while(!currentLine.empty()){ + Person person; + //Add all characters to the vector + for(char ch : currentLine){ + person.yeses.push_back(ch); + } + //Add the person to the group + group.people.push_back(person); + //Get the next line + std::getline(inputFile, currentLine); + } + //When a blank line is found move to a new group + groups.push_back(group); + } + + //Return all of the groups in the file + return groups; +} + + +int main(){ + mee::Stopwatch timer; + std::vector yeses; + + //Get the input from the file + std::vector groups = getInput(); + + + //Start the timer + timer.start(); + + //Go through every group, get the number of yeses, and add it to the sum + for(Group group : groups){ + yeses.push_back(group.getUniversalYeses().size()); + } + //Get the sum of the number of yeses in all groups + int sum = mee::getSum(yeses); + + + //Stop the timer + timer.stop(); + + //Print the results + std::cout << "The sum of all yeses is " << sum + << "\nIt took " << timer.getStr() << " to finish this problem" << std::endl; + + return 0; +} + +/* Results: +The sum of all yeses is 3356 +It took 2.998 milliseconds to finish this problem +*/