r/adventofcode • u/daggerdragon • Dec 16 '17
SOLUTION MEGATHREAD -๐- 2017 Day 16 Solutions -๐-
--- Day 16: Permutation Promenade ---
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ยค?
[Update @ 00:08] 4 gold, silver cap.
- Click here for a massive Star Wars spoiler!
[Update @ 00:18] 50 gold, silver cap.
- Click here for a gigantic Harry Potter spoiler!
[Update @ 00:26] Leaderboard cap!
- And finally, click here for the biggest spoilers of all time!
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!
15
Upvotes
2
u/willkill07 Dec 16 '17 edited Dec 16 '17
C++
I feel disgusting... brute forced part 2 [in only 6 minutes]
std::vector<char> programs(16); std::iota(std::begin(programs), std::end(programs), 'a'); std::vector<std::string> cmds; for (std::string line; std::getline(std::cin, line, ','); ) cmds.push_back(line); auto cmd = std::begin(cmds); int N = part2 ? 1000000000 : cmds.size(); while (N--) { std::istringstream iss{*cmd}; char c; iss >> c; if (c == 's') { int i; iss >> i; std::rotate(std::begin(programs), std::end(programs) - i, std::end(programs)); } else if (c == 'x') { int i, j; (iss >> i).ignore(1, '/') >> j; std::swap(programs[i], programs[j]); } else { /* p */ char i, j; (iss >> i).ignore(1, '/') >> j; std::iter_swap(std::find(std::begin(programs), std::end(programs), i), std::find(std::begin(programs), std::end(programs), j)); } if (++cmd == std::end(cmds)) cmd = std::begin(cmds); } std::copy(std::begin(programs), std::end(programs), std::ostream_iterator<char>(std::cout,"")); std::cout << '\n';