#include "../aoc2022.h" #define LOWEROFFSET 96 #define UPPEROFFSET 38 enum ECheckType { eIndividual, eGrouped }; template struct SDay03 : public SDay { //Class constructor using SDay::SDay; //Solutions to both days Output part1() override final { return readBackpacks(eIndividual); } Output part2() override final { return readBackpacks(eGrouped); } int readBackpacks(ECheckType checkType) { this->input.clear(); this->input.seekg(0, std::ios::beg); std::string line{ "" }; int total{ 0 }; int counter{ 0 }; std::string bags[3]; while (std::getline(this->input, line)) { if (checkType == eIndividual) {//Checking an individual bag std::string compartment1, compartment2; for (int i = 0; i < line.size(); i++) { if (i < (line.size() / 2)) compartment1 += line[i]; else compartment2 += line[i]; } //Find the items that appear in both backpacks and get priority std::map inCompartment1 = getMap(compartment1); std::map duplicates = findDuplicates(inCompartment1, compartment2); total += getDuplicatePriority(duplicates); } else {//Checking a group of bags counter++; bags[counter % 3] = line; if (counter % 3 == 0) { std::map bag1 = getMap(bags[0]); std::map bag2 = findDuplicates(bag1, bags[1]); std::map bag3 = findDuplicates(bag2, bags[2]); total += getDuplicatePriority(bag3); } } } return total; } //Function that take in a prefilled map and checks through a string for duplicates std::map findDuplicates(std::map& duplicates, std::string backpack) { std::map newDuplicates; for (int i = 0; i < backpack.size(); i++) { if (duplicates[backpack[i]] && newDuplicates.count(backpack[i]) == 0) {//If the item is in bothcompartments newDuplicates[backpack[i]] = duplicates[backpack[i]]; } } return newDuplicates; } //Function that takes in a string and generates a map of all of the items in it std::map getMap(std::string &backpack) { std::map characters; for (int i = 0; i < backpack.size(); i++) { characters[backpack[i]] = getItemPriority(backpack[i]); } return characters; } //Function that goes through a map and returns the total priority for the maps int getDuplicatePriority(std::map &duplicates) { int total{ 0 }; for (auto& i : duplicates) {//Add the sum of the duplicate items total += i.second; } return total; } //Get the proiority of an item in the backpack int getItemPriority(char item) { if (islower(item)) return item - LOWEROFFSET; return item - UPPEROFFSET; } };