Completed Day3 - Very verbose code, could do with some cleanup
This commit is contained in:
+101
-2
@@ -1,11 +1,110 @@
|
||||
#include "../aoc2022.h"
|
||||
|
||||
#define LOWEROFFSET 96
|
||||
#define UPPEROFFSET 38
|
||||
|
||||
enum ECheckType {
|
||||
eIndividual,
|
||||
eGrouped
|
||||
};
|
||||
|
||||
template <typename Output>
|
||||
struct SDay03 : public SDay<Output> {
|
||||
//Class constructor
|
||||
using SDay<Output>::SDay;
|
||||
|
||||
//Solutions to both days
|
||||
Output part1() override final { return Output(); }
|
||||
Output part2() override final { return Output(); }
|
||||
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<char, int> inCompartment1 = getMap(compartment1);
|
||||
std::map<char, int> duplicates = findDuplicates(inCompartment1, compartment2);
|
||||
total += getDuplicatePriority(duplicates);
|
||||
}
|
||||
else
|
||||
{//Checking a group of bags
|
||||
counter++;
|
||||
bags[counter % 3] = line;
|
||||
if (counter % 3 == 0)
|
||||
{
|
||||
std::map<char, int> bag1 = getMap(bags[0]);
|
||||
std::map<char, int> bag2 = findDuplicates(bag1, bags[1]);
|
||||
std::map<char, int> 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<char, int> findDuplicates(std::map<char, int>& duplicates, std::string backpack) {
|
||||
std::map<char, int> 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<char, int> getMap(std::string &backpack) {
|
||||
std::map<char, int> 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<char, int> &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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user