You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AOC2022/03/p2_solution.cpp

62 lines
1.6 KiB

#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int intoPriority(char ch)
{
int initalP = ch;
if (initalP < 97)
{
initalP -= 38;
} else {initalP -= 96;}
return initalP;
}
int main()
{
string inputStr;
ifstream inputFile("input.txt");
int i {0};
// This will be reset every 3 lines
map<char,int> itemCounts;
int badgePrioity {0};
while (getline(inputFile, inputStr))
{
i++;
// These are the items we've seen this line
vector<char> seenVec;
// Increment the item counter for each item not already seen in this line
for (char c: inputStr)
{
// Check that we haven't already seen this char
if (find(seenVec.begin(), seenVec.end(), c) != seenVec.end()) {continue;}
seenVec.push_back(c);
itemCounts[c]++;
}
// If this is the 3rd line we need to check for the badge and reset itemCounts
if (i%3==0)
{
map<char, int>::iterator itr;
// Look through the map for something that occured three times
for (itr = itemCounts.begin(); itr != itemCounts.end(); itr++)
{
if (itr->second == 3)
{
// Add that items value to our total
badgePrioity += intoPriority(itr->first);
}
}
// Erase everything in the count map as we're now in a new group
itemCounts.clear();
}
}
cout << badgePrioity << endl;
}