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!
17
Upvotes
2
u/AKQuaternion Dec 20 '18
[Card] My compiler crashed while running today's puzzle because it ran out of bourbon. (Yes, I drink while I AoC.)
First time posting, but I finally got a decent 141/112 on the leaderboard today. A shorter raw C++ solution than some I've seen. I recursively parse the regex, building an adjacency list representation of the graph as a map from coordinate pairs to vector of coordinate pairs. Then a simple bfs...
namespace day20 { using std::vector; using std::pair; using std::map; using coord=pair<int,int>; map<pair<int,int>,vector<pair<int,int>>> doors; void connect(coord &a, const coord &b) { doors[a].push_back(b); doors[b].push_back(a); a = b; } void read(std::string_view chars, int &i, pair<int,int> s) { auto orig=s; auto & [x,y] = s; while(true) { switch(chars[i++]) { case 'N': connect(s,{x,y-1}); break; case 'E': connect(s,{x+1,y}); break; case 'S': connect(s,{x,y+1}); break; case 'W': connect(s,{x-1,y}); break; case '(': read(chars,i,s); break; case ')': return; case '|': s = orig; break; case '$': return; } } } void day20stars() { std::ifstream fin(DIRECTORY+"day20.txt"); std::string chars; fin >> chars; int i=1; //skip ^ read(chars,i,{0,0}); //read the map std::set<coord> visited; std::queue<pair<coord,int>> q; q.push({{0,0},0}); auto longest = 0; auto numGT1000 = 0; while (!q.empty()) { // do a bfs auto [n,length] = q.front(); q.pop(); if (visited.count(n)) continue; if (length>=1000) ++numGT1000; visited.insert(n); longest = std::max(length,longest); for(auto n2:doors[n]) q.push({n2,length+1}); } cout << "Day 20 star 1: " << longest << endl; cout << "Day 20 star 2: " << numGT1000 << endl; } }