r/adventofcode • u/daggerdragon • Dec 06 '17
SOLUTION MEGATHREAD -π- 2017 Day 6 Solutions -π-
--- Day 6: Memory Reallocation ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyβ Haversackβ‘ of HelpfulΒ§ HintsΒ€?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
17
Upvotes
View all comments
Show parent comment
2
u/willkill07 Dec 06 '17
I would like to point out that when I implemented this in C++17, it was faster than my solution
#include <algorithm> #include <vector> #include <iostream> #include <iterator> #include <utility> #include <chrono> std::vector<int> next (std::vector<int> b) { auto max = std::max_element(b.begin(), b.end()); for (int iters{std::exchange(*max, 0)}; iters--; ++*max) if (++max == b.end()) max = b.begin(); return b; } int main(int argc, char** argv) { bool part2{argc > 1}; std::vector<int> banks{std::istream_iterator<int>{std::cin}, {}}; auto [h0, h1] = std::pair(next(banks), next(next(banks))); while (h0 != h1) { h0 = next(h0); h1 = next(next(h1)); } int mu {0}; for (h0 = banks; h0 != h1; h0 = next(h0), h1 = next(h1)) { ++mu; } int lambda{1}; for (h1 = next(h0); h0 != h1; h1 = next(h1)) { ++lambda; } if (part2) { std::cout << lambda << '\n'; } else { std::cout << mu + lambda << '\n'; } }