Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <sstream>
- #include <vector>
- #include <valarray>
- #include <map>
- #include <cmath>
- using namespace std;
- int main() {
- string line;
- getline(cin, line);
- transform(line.begin(), line.end(), line.begin(), [](char ch) {return ch == ',' ? ' ' : ch;});
- istringstream ss(line);
- vector<int> crab_hpos;
- copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(crab_hpos));
- valarray<int> orig(&crab_hpos[0], crab_hpos.size());
- int c_begin = orig.min(), c_end = orig.max();
- map<int, int> ranked;
- // It's the triangle number, now. For diff of 4, it's 10.
- for (int p = c_begin; p <= c_end; ++p) {
- int diff = (orig - p).apply(abs).apply([] (int n) {return (n * (n + 1)) / 2;}).sum();
- ranked[diff] = p;
- }
- cout << "Ranked choices:" << endl;
- for (auto const & rc : ranked) {
- cout << rc.first << ": " << rc.second << endl;
- }
- cout
- << "Winning position: " << ranked.begin()->second
- << " using " << ranked.begin()->first << " fuel" << endl;
- }
Add Comment
Please, Sign In to add comment