#include #include #include #include #include using std::find_if; using std::istream; using std::ostream; using std::size_t; using std::sort; using std::uint64_t; using std::vector; using std::distance; auto Main(istream & in, ostream & out) -> void { vector L1, L2; { uint64_t x, y; while (in >> x >> y) { L1.push_back(x); L2.push_back(y); } } sort(begin(L1), end(L1)); sort(begin(L2), end(L2)); uint64_t p1 = 0; for (size_t i = 0; i < L1.size(); i++) { p1 += L1[i] > L2[i] ? L1[i] - L2[i] : L2[i] - L1[i]; } uint64_t p2 = 0; for (auto i = begin(L1), j = begin(L2); i < end(L1) && j < end(L2); ) { if (*i < *j) { i++; } else if (*j < *i) { j++; } else { auto const p = [t = *i](auto const x) { return x != t; }; auto const i_ = find_if(i, end(L1), p); auto const j_ = find_if(j, end(L2), p); p2 += *i * (i_ - i) * (j_ - j); i = i_; j = j_; } } out << "Part 1: " << p1 << "\n" << "Part 2: " << p2 << "\n"; }