Terrible C++ solution for part 1 !
#include <iostream> #include <fstream> #include <list> #include <map> std::list<std::string> read_file(std::string filename) { auto lines = std::list<std::string>(); std::ifstream file(filename); for (std::string line; std::getline(file, line);) { lines.push_back(line); } file.close(); return lines; } class WordCounter { public: static std::map<char, int> CalculateLine(const std::string line) { auto map = std::map<char, int>(); if (line.length() == 0) { return map; } for (int i = 0; i < line.length(); i++) { std::map<char, int>::iterator it = map.find(line[i]); if (it != map.end()) { it->second = it->second + 1; } else { map.insert(std::pair<char, int>(line[i], 1)); } } return map; } static const std::pair<int, int> Sum(std::map<char, int> map) { int twos = 0; int threes = 0; for (auto line = map.begin(); line != map.end(); line++) { if (line->second == 2) { twos = 1; } else if (line->second == 3) { threes = 1; } } return std::pair<int, int>(twos, threes); } }; int main() { auto input = read_file("./input.txt"); int two = 0; int three = 0; for (auto i = input.begin(); i != input.end(); i++) { auto letterMap = WordCounter::CalculateLine(*i); auto calculated = WordCounter::Sum(letterMap); two += calculated.first; three += calculated.second; } std::cout << two * three << std::endl; return 0; }
Terrible is better than never finished! And this looks pretty good to me, not knowing C++ if that makes you feel better 😄
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Terrible C++ solution for part 1 !
Terrible is better than never finished! And this looks pretty good to me, not knowing C++ if that makes you feel better 😄