Guest User

Untitled

a guest
Oct 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <iterator>
  8. #include <string>
  9. #include <experimental/filesystem>
  10.  
  11. namespace fs = std::experimental::filesystem;
  12.  
  13. uint64_t calc_answer(const std::list<int>& list)
  14. {
  15. int left_sum = 0;
  16. int right_sum = std::accumulate(list.begin(), list.end(), 0);
  17.  
  18. uint64_t index_sum = 0;
  19. int curr_index = 0;
  20.  
  21. std::for_each(list.begin(), list.end(), [&] (int val) {
  22. right_sum -= val;
  23.  
  24. if(left_sum == right_sum)
  25. index_sum += curr_index;
  26.  
  27. left_sum += val;
  28.  
  29. ++curr_index;
  30. });
  31.  
  32. return index_sum;
  33. }
  34.  
  35. std::string read_file(const fs::path& file_path)
  36. {
  37. if(!fs::exists(file_path))
  38. return "";
  39.  
  40. std::ifstream infile{file_path.string()};
  41.  
  42. std::string data{""};
  43.  
  44. std::string row{""};
  45. while(std::getline(infile, row))
  46. data.append(row);
  47.  
  48. return data;
  49. }
  50.  
  51. struct Facet : std::ctype<char>
  52. {
  53. using ParentT = std::ctype<char>;
  54.  
  55. Facet(std::size_t refs = 0) : ParentT(make_table(), false, refs) {}
  56.  
  57. static const mask* make_table()
  58. {
  59. static mask char_table[table_size];
  60. static int fake = (std::copy(classic_table(), classic_table() + table_size, char_table),
  61. (void)fake, 0);
  62.  
  63. char_table[','] |= space;
  64.  
  65. return char_table;
  66. }
  67. };
  68.  
  69. std::list<int> parse_raw_data(const std::string& raw_data)
  70. {
  71. std::istringstream iss{raw_data};
  72. iss.imbue(std::locale{iss.getloc(), new Facet{}});
  73.  
  74. std::list<int> data{};
  75.  
  76. using ISIt = std::istream_iterator<int>;
  77. std::copy(ISIt{iss}, ISIt{}, std::back_inserter(data));
  78.  
  79. return data;
  80. }
  81.  
  82. int main()
  83. {
  84. const fs::path DATA_PATH{"data.txt"};
  85.  
  86. std::string raw_data = read_file(DATA_PATH);
  87.  
  88. if(raw_data.empty())
  89. {
  90. std::cout << "File 'data.txt' not found or empty. Terminate..." << std::endl;
  91. return 0;
  92. }
  93.  
  94. std::list<int> data = parse_raw_data(raw_data);
  95.  
  96. std::cout << "Sum of indexes that satisfies specified propery: " << calc_answer(data) << std::endl;
  97.  
  98. return 0;
  99. }
  100.  
  101. #include <iostream>
  102. #include <list>
  103. #include <algorithm>
  104. #include <numeric>
  105. #include <fstream>
  106. #include <sstream>
  107. #include <iterator>
  108. #include <string>
  109. #include <experimental/filesystem>
  110.  
  111. namespace fs = std::experimental::filesystem;
  112.  
  113. uint64_t calc_answer(const std::list<int>& list)
  114. {
  115. int left_sum = 0;
  116. int right_sum = std::accumulate(list.begin(), list.end(), 0);
  117.  
  118. uint64_t index_sum = 0;
  119. int curr_index = 0;
  120.  
  121. std::for_each(list.begin(), list.end(), [&] (int val) {
  122. right_sum -= val;
  123.  
  124. if(left_sum == right_sum)
  125. index_sum += curr_index;
  126.  
  127. left_sum += val;
  128.  
  129. ++curr_index;
  130. });
  131.  
  132. return index_sum;
  133. }
  134.  
  135. std::list<int> read_file(const fs::path& file_path)
  136. {
  137. if(!fs::exists(file_path))
  138. return std::list<int>{};
  139.  
  140. std::ifstream infile{file_path.string()};
  141.  
  142. std::list<int> data{};
  143.  
  144. std::string number{""};
  145. while(std::getline(infile, number, ','))
  146. data.push_back(std::stoi(number));
  147.  
  148. return data;
  149. }
  150.  
  151. int main()
  152. {
  153. const fs::path DATA_PATH{"data.txt"};
  154.  
  155. std::list<int> data = read_file(DATA_PATH);
  156.  
  157. if(data.empty())
  158. {
  159. std::cout << "File 'data.txt' not found or empty. Terminate..." << std::endl;
  160. return 0;
  161. }
  162.  
  163. std::cout << "Sum of indexes that satisfies specified propery: "
  164. << calc_answer(data) << std::endl;
  165.  
  166. return 0;
  167. }
Add Comment
Please, Sign In to add comment