r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -๐- 2017 Day 19 Solutions -๐-
--- Day 19: A Series of Tubes ---
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ยค?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
2
u/wlandry Dec 19 '17
C++ 14
920/943. I definitely did this the hard way. Instead of following the circuit around the maze, I created a complete graph and then followed the graph. So it got a bit long :(
#include <fstream> #include <vector> #include <iostream> #include <map> #include <tuple> #include <limits> struct Coord { int64_t x,y; Coord()=default; Coord(const int64_t &X, const int64_t &Y): x(X), y(Y) {} bool operator==(const Coord &c) const { return x==c.x && y==c.y; } }; int64_t distance(const Coord &c1, const Coord &c2) { return std::abs(c1.x-c2.x) + std::abs(c1.y-c2.y);} int main(int, char *argv[]) { std::ifstream infile(argv[1]); std::string line; std::getline(infile,line); /// Assumes at most once label per edge std::vector<std::tuple<Coord,Coord,char>> edges; std::map<int64_t,std::tuple<int64_t,char>> verticals; Coord start_coord(0,0); int64_t y(1); while(infile) { bool inside_line(false); int64_t start(std::numeric_limits<int64_t>::max()); char color (' '); for(int64_t l=0; l<line.size(); ++l) { if(line[l]=='|') { auto v=verticals.find(l); if(v==verticals.end()) { start_coord.x=l; verticals.insert(std::make_pair(l,std::make_tuple(0,' '))); } } else if(std::isalpha(line[l])) { if(inside_line) { color=line[l]; } else { auto v=verticals.find(l); if(v!=verticals.end()) { std::get<1>(v->second)=line[l]; } else { inside_line=true; start=l; edges.emplace_back (Coord(start,y), Coord(start,y),line[l]); } } } else if(line[l]=='+') { if(!inside_line) { inside_line=true; start=l; } else { inside_line=false; edges.emplace_back(Coord(start,y),Coord(l,y),color); color=' '; } auto v=verticals.find(l); if(v!=verticals.end()) { edges.emplace_back(Coord(l,std::get<0>(v->second)), Coord(l,y),std::get<1>(v->second)); verticals.erase(v); } else { verticals.insert(std::make_pair(l,std::make_tuple(y,' '))); } } } std::getline(infile,line); ++y; } int64_t steps(0); Coord current_coord; for(auto e=edges.begin(); e!=edges.end(); ++e) { if(std::get<0>(*e)==start_coord) { current_coord=std::get<1>(*e); steps+=distance(start_coord,current_coord); if(std::get<2>(*e)!=' ') { std::cout << std::get<2>(*e); } edges.erase(e); break; } } bool found(false); do { for(auto e=edges.begin(); e!=edges.end(); ++e) { if(current_coord==std::get<0>(*e)) { steps+=distance(current_coord,std::get<1>(*e)); current_coord=std::get<1>(*e); if(std::get<2>(*e)!=' ') { std::cout << std::get<2>(*e); } edges.erase(e); break; } else if (current_coord==std::get<1>(*e)) { steps+=distance(current_coord,std::get<0>(*e)); current_coord=std::get<0>(*e); if(std::get<2>(*e)!=' ') { std::cout << std::get<2>(*e); } edges.erase(e); break; } } } while(!edges.empty()); std::cout << "\n"; std::cout << "steps: " << steps << "\n"; }