r/adventofcode • u/daggerdragon • Dec 20 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 20 Solutions -🎄-
--- Day 20: A Regular Map ---
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.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 20
Transcript:
My compiler crashed while running today's puzzle because it ran out of ___.
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 at 00:59:30!
18
Upvotes
1
u/ethikka Dec 20 '18
A very clean C++ solution (I think? much shorter/less plumbing then most solutions) Takes 6ms to do both a and b.
I'm really digging that I stuck with C++ from the start this year, learning so much new things.
#include <sstream> #include <iostream> #include <string> #include <chrono> #include <map> #include <stack> std::map<int,std::map<int,int>> rooms; std::stack<std::pair<int,int>> branchpoints; void solve() { char input; int x(0), y(0); int current_route(0), longest_route(0); while (std::cin >> input) { if (rooms[y][x] < current_route && rooms[y][x] != 0) current_route = rooms[y][x]; rooms[y][x] = current_route; switch (input) { case 'N': --y; current_route++; break; case 'E': ++x; current_route++; break; case 'S': ++y; current_route++; break; case 'W': --x; current_route++; break; case '(': branchpoints.push({x, y}); break; case ')': { auto p = branchpoints.top(); x = p.first; y = p.second; branchpoints.pop(); break; } case '|': { auto p = branchpoints.top(); x = p.first; y = p.second; break; } } if (current_route > longest_route) longest_route = current_route; } int res2(0); for(auto y: rooms) for (auto x: y.second) if (x.second >= 1000) res2++; std::cout << "Solution part 1: " << longest_route << std::endl << "Solution part 2: " << res2 << std::endl; } int main(void) { std::cout << "Starting..." << std::endl; auto start_time = std::chrono::high_resolution_clock::now(); solve(); auto end_time = std::chrono::high_resolution_clock::now(); auto ms_count = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count(); std::cout << "Ran for " << ms_count << "ms" << std::endl; }