fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <map>
  6. #include <curl/curl.h>
  7. #include <algorithm>
  8.  
  9. static size_t WriteCallBack(void* contents, size_t size, size_t mem, void* user){
  10. ((std::string*)user)->append((char*) contents, size * mem);
  11. return size * mem;
  12. }
  13.  
  14. std::string GetDocContent(const std::string& url){
  15. CURL* curl;
  16. CURLcode result;
  17. std::string buffer;
  18.  
  19. std::string content;
  20.  
  21. curl = curl_easy_init();
  22.  
  23. if (!curl) {
  24. std::cerr << "Failed to init curl." << std::endl;
  25. return content;
  26. }
  27.  
  28. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  29. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  30. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack);
  31. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  32.  
  33. result = curl_easy_perform(curl);
  34.  
  35. if (result != CURLE_OK){
  36. std::cerr << "Curl easy perform failed: " << curl_easy_strerror(result) << std::endl;
  37. }
  38.  
  39. curl_easy_cleanup(curl);
  40.  
  41. return buffer;
  42. }
  43.  
  44. std::vector<std::string> Split(const std::string& line, char delimiter){
  45. std::stringstream ss(line);
  46. std::string obj;
  47. std::vector<std::string> elems;
  48.  
  49. while (std::getline(ss, obj, delimiter)){
  50. elems.push_back(obj);
  51. }
  52.  
  53. return elems;
  54. }
  55.  
  56. void PrintMessage(const std::string& url){
  57. std::string content = GetDocContent(url);
  58.  
  59. if(content.empty()){
  60. std::cerr << "Failed to retrive website data." << std::endl;
  61. return;
  62. }
  63.  
  64. std::istringstream iss(content);
  65. std::string line;
  66. bool beginTable = false;
  67. std::vector<std::tuple<int, char, int>> coords;
  68.  
  69. int maxX = 0;
  70. int maxY = 0;
  71.  
  72. while (std::getline(iss, line)){
  73. if (!beginTable){
  74. if (line.find("The table below contains the input data needed to solve the coding assessment exercise.") != std::string::npos){
  75. beginTable = true;
  76. }
  77. }
  78. continue;
  79.  
  80. if (!line.empty()){
  81. std::vector<std::string> info = Split(line, '\t');
  82. if (info.size() == 3){
  83. int x = std::stoi(info[0]);
  84. char ch = info[1][0];
  85. int y = std::stoi(info[2]);
  86.  
  87. coords.emplace_back(x, ch, y);
  88. maxX = std::max(maxX, x);
  89. maxY = std::max(maxY, y);
  90. }
  91. }
  92. }
  93.  
  94.  
  95.  
  96. std::vector<std::vector<char>> grid(maxY + 1, std::vector<char>(maxX + 1, ' '));
  97.  
  98. for (const auto& [x, ch, y] : coords){
  99. grid[y][x] = ch;
  100. }
  101.  
  102. for (const auto& row : grid){
  103. for(char ch : row){
  104. std::cout << ch;
  105. }
  106. std::cout << '\n';
  107. }
  108.  
  109. }
  110.  
  111. int main(){
  112. std::string docURL = "https://docs.google.com/document/d/e/2PACX-1vTER-wL5E8YC9pxDx43gk8eIds59GtUUk4nJo_ZWagbnrH0NFvMXIw6VWFLpf5tWTZIT9P9oLIoFJ6A/pub";
  113. PrintMessage(docURL);
  114. return 0;
  115. }
  116.  
Success #stdin #stdout 0.02s 25932KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <curl/curl.h>
#include <algorithm>

static size_t WriteCallBack(void* contents, size_t size, size_t mem, void* user){
    ((std::string*)user)->append((char*) contents, size * mem);
    return size * mem;
}

std::string GetDocContent(const std::string& url){
    CURL* curl;
    CURLcode result;
    std::string buffer;

    std::string content;
    
    curl = curl_easy_init();
    
    if (!curl) {
        std::cerr << "Failed to init curl." << std::endl;
        return content;
    }

    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

    result = curl_easy_perform(curl);

    if (result != CURLE_OK){
        std::cerr << "Curl easy perform failed: " << curl_easy_strerror(result) << std::endl;
    }

    curl_easy_cleanup(curl);

    return buffer;
}

std::vector<std::string> Split(const std::string& line, char delimiter){
    std::stringstream ss(line);
    std::string obj;
    std::vector<std::string> elems;

    while (std::getline(ss, obj, delimiter)){
        elems.push_back(obj);
    }

    return elems;
}

void PrintMessage(const std::string& url){
    std::string content = GetDocContent(url);

    if(content.empty()){
        std::cerr << "Failed to retrive website data." << std::endl;
        return;
    }

    std::istringstream iss(content);
    std::string line;
    bool beginTable = false;
    std::vector<std::tuple<int, char, int>> coords;

    int maxX = 0;
    int maxY = 0;

    while (std::getline(iss, line)){
        if (!beginTable){
            if (line.find("The table below contains the input data needed to solve the coding assessment exercise.") != std::string::npos){
                beginTable = true;
            }
        }
        continue;

        if (!line.empty()){
            std::vector<std::string> info = Split(line, '\t');
            if (info.size() == 3){
                int x = std::stoi(info[0]);
                char ch = info[1][0];
                int y = std::stoi(info[2]);

                coords.emplace_back(x, ch, y);
                maxX = std::max(maxX, x);
                maxY = std::max(maxY, y);
            }
        }
    }

    

    std::vector<std::vector<char>> grid(maxY + 1, std::vector<char>(maxX + 1, ' '));

    for (const auto& [x, ch, y] : coords){
        grid[y][x] = ch;
    }

    for (const auto& row : grid){
        for(char ch : row){
            std::cout << ch;
        }
        std::cout << '\n';
    }

}

int main(){
    std::string docURL = "https://docs.google.com/document/d/e/2PACX-1vTER-wL5E8YC9pxDx43gk8eIds59GtUUk4nJo_ZWagbnrH0NFvMXIw6VWFLpf5tWTZIT9P9oLIoFJ6A/pub";
    PrintMessage(docURL);
    return 0;
}