Guest User

Untitled

a guest
Dec 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5.  
  6. int main() {
  7. int twos = 0;
  8. int threes = 0;
  9.  
  10. std::string line;
  11.  
  12. std::ifstream inFile;
  13. inFile.open("aoc-input-2");
  14.  
  15. while (inFile >> line) {
  16. std::map<char, int> counts;
  17.  
  18. for(char& c : line) {
  19. if(counts.count(c) == 0) {
  20. counts[c] = 1;
  21. } else {
  22. ++counts[c];
  23. }
  24. }
  25.  
  26. bool foundTwos = false;
  27. bool foundThrees = false;
  28.  
  29. auto it = counts.begin();
  30.  
  31. while(it != counts.end()) {
  32. if (!foundTwos && it->second == 2) {
  33. ++twos;
  34. foundTwos = true;
  35.  
  36. if (foundThrees) break;
  37. } else if (!foundThrees && it->second == 3) {
  38. ++threes;
  39. foundThrees = true;
  40.  
  41. if (foundTwos) break;
  42. }
  43.  
  44. ++it;
  45. }
  46. }
  47. inFile.close();
  48.  
  49. std::cout << (twos * threes) << std::endl;
  50. }
Add Comment
Please, Sign In to add comment