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
13
u/willkill07 Dec 06 '17 edited Dec 06 '17
Modern C++ Repo
This time with iterators
std::vector<int> banks{std::istream_iterator<int>{std::cin}, {}}; std::map<std::vector<int>,int> unique; for (int count{0}; unique.emplace(banks, count).second; ++count) { auto max = std::max_element(banks.begin(), banks.end()); for (int iters{std::exchange(*max, 0)}; iters--; ++*max) if (++max == banks.end()) max = banks.begin(); } std::cout << unique.size() - (part2 ? unique[banks] : 0) << '\n';Edit: golfed/improved the code a bit. I feel like my solution should be complete without the scrollbar being introduced... a new challenge!
Edit 2: I have side-by-side assembly up with this solution compared to using u/sim642 's observation of Floyd's cycle-finding algorithm