Getting distance between two elements in a 2D map - not quite correct -solved by Brian
I am working on the 2D Map challenge, so careful spoiler alert... Anyways, somehow my solution is flawed but I do not know where the mistake is. Any help or hints would be appreciated :) Here is the code: #include<iostream> #include<string> #include<vector> #include<sstream> #include<algorithm> int main() { std::string str = "PXXXX,XXXXX,XXXXP,XXXXX,XXXXX"; //std::string str; //std::getline(std::cin, str); std::vector<std::string> vec; std::stringstream ss(str); std::string strTemp; std::vector<int> nums; std::vector<int> block; std::vector<int> option; int result = 0; //first tokenize strings and create vector of strings while(std::getline(ss, strTemp, ',')) { vec.push_back(strTemp); } //find index of 'P' in each string for(auto &val : vec) { std::cout << val << std::endl; std::size_t found = val.find('P'); if (found != std::string::npos) { nums.push_back(found); } } //find instance of 'P' in string for(size_t i = 0; i < vec.size(); i++) { if (std::find(vec[i].begin(), vec[i].end(), 'P') != vec[i].end()) { block.push_back(i); } } //if two Ps are found in the same string... if(block.size()<2) { for(unsigned int i = 0; i< vec[block[0]].size(); i++) { if(vec[block[0]][i] == 'P') { option.push_back(i); } } result = option[1] - option[0]; std::cout << result; } //subtract position of indices and position in blocks and add result else { result = (nums[1]-nums[0]) + (block[1]-block[0]); std::cout << result; } }