Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <algorithm>
- #include <numeric>
- #include <fstream>
- #include <sstream>
- #include <iterator>
- #include <string>
- #include <experimental/filesystem>
- namespace fs = std::experimental::filesystem;
- uint64_t calc_answer(const std::list<int>& list)
- {
- int left_sum = 0;
- int right_sum = std::accumulate(list.begin(), list.end(), 0);
- uint64_t index_sum = 0;
- int curr_index = 0;
- std::for_each(list.begin(), list.end(), [&] (int val) {
- right_sum -= val;
- if(left_sum == right_sum)
- index_sum += curr_index;
- left_sum += val;
- ++curr_index;
- });
- return index_sum;
- }
- std::string read_file(const fs::path& file_path)
- {
- if(!fs::exists(file_path))
- return "";
- std::ifstream infile{file_path.string()};
- std::string data{""};
- std::string row{""};
- while(std::getline(infile, row))
- data.append(row);
- return data;
- }
- struct Facet : std::ctype<char>
- {
- using ParentT = std::ctype<char>;
- Facet(std::size_t refs = 0) : ParentT(make_table(), false, refs) {}
- static const mask* make_table()
- {
- static mask char_table[table_size];
- static int fake = (std::copy(classic_table(), classic_table() + table_size, char_table),
- (void)fake, 0);
- char_table[','] |= space;
- return char_table;
- }
- };
- std::list<int> parse_raw_data(const std::string& raw_data)
- {
- std::istringstream iss{raw_data};
- iss.imbue(std::locale{iss.getloc(), new Facet{}});
- std::list<int> data{};
- using ISIt = std::istream_iterator<int>;
- std::copy(ISIt{iss}, ISIt{}, std::back_inserter(data));
- return data;
- }
- int main()
- {
- const fs::path DATA_PATH{"data.txt"};
- std::string raw_data = read_file(DATA_PATH);
- if(raw_data.empty())
- {
- std::cout << "File 'data.txt' not found or empty. Terminate..." << std::endl;
- return 0;
- }
- std::list<int> data = parse_raw_data(raw_data);
- std::cout << "Sum of indexes that satisfies specified propery: " << calc_answer(data) << std::endl;
- return 0;
- }
- #include <iostream>
- #include <list>
- #include <algorithm>
- #include <numeric>
- #include <fstream>
- #include <sstream>
- #include <iterator>
- #include <string>
- #include <experimental/filesystem>
- namespace fs = std::experimental::filesystem;
- uint64_t calc_answer(const std::list<int>& list)
- {
- int left_sum = 0;
- int right_sum = std::accumulate(list.begin(), list.end(), 0);
- uint64_t index_sum = 0;
- int curr_index = 0;
- std::for_each(list.begin(), list.end(), [&] (int val) {
- right_sum -= val;
- if(left_sum == right_sum)
- index_sum += curr_index;
- left_sum += val;
- ++curr_index;
- });
- return index_sum;
- }
- std::list<int> read_file(const fs::path& file_path)
- {
- if(!fs::exists(file_path))
- return std::list<int>{};
- std::ifstream infile{file_path.string()};
- std::list<int> data{};
- std::string number{""};
- while(std::getline(infile, number, ','))
- data.push_back(std::stoi(number));
- return data;
- }
- int main()
- {
- const fs::path DATA_PATH{"data.txt"};
- std::list<int> data = read_file(DATA_PATH);
- if(data.empty())
- {
- std::cout << "File 'data.txt' not found or empty. Terminate..." << std::endl;
- return 0;
- }
- std::cout << "Sum of indexes that satisfies specified propery: "
- << calc_answer(data) << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment