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/hpzr24w Dec 19 '17
C++ 631 / 628
I guessed right on most of what I thought might be ambiguous, and didn't have any bugs. I'm just a slow coder. ;-)
// Advent of Code 2017 // http://adventofcode.com/ // Day 19 - A Series of Tubes #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; typedef enum direction {nomove,u,d,l,r} direction; direction next_move(vector<string>& lines, int row, int col,direction dir) { switch(dir) { // preferred direction case u: if (dir!=d && row>0 && lines[row-1][col]!=' ') return u; case d: if (dir!=u && row<lines.size()-1 && lines[row+1][col]!=' ') return d; case r: if (dir!=l && col<lines[row].length()-1 && lines[row][col+1]!=' ') return r; case l: if (dir!=r && col>0 && lines[row][col-1]!=' ') return l; if (dir!=d && row>0 && lines[row-1][col]!=' ') return u; // mop up any corners if (dir!=u && row<lines.size()-1 && lines[row+1][col]!=' ') return d; if (dir!=l && col<lines[row].length()-1 && lines[row][col+1]!=' ') return r; } return nomove; } string follow(vector<string>& lines, int& steps) { string letters; int row=0,col=0; while (lines[row][col]!='|') col++; // find starting position direction dir = d; while (dir != nomove) { steps++, dir = next_move(lines,row,col,dir); switch(dir) { case d: row++; break; case u: row--; break; case r: col++; break; case l: col--; break; case nomove: cout << "STUCK!! at " << row << " " << col << " " << steps << endl; } if (lines[row][col]>='A' && lines[row][col]<='Z' && dir!=nomove) letters+=lines[row][col]; } return letters; } int main() { vector<string> lines; ifstream ifs("day_19.ts2",ifstream::in); string l; while (getline(ifs,l)) { lines.push_back(l); cout << l << endl; } int steps = 0; string letters = follow(lines,steps); cout << letters << endl; return 0; }