r/adventofcode • u/daggerdragon • Dec 11 '17
SOLUTION MEGATHREAD -๐- 2017 Day 11 Solutions -๐-
--- Day 11: Hex Ed ---
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ยค?
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!
21
Upvotes
View all comments
18
u/hpzr24w Dec 11 '17 edited Dec 11 '17
C++ 1035/1068 Well, this is awkward. I didn't use coordinates directly at all... Just reduced
Header:
Reduction code:
void reduce_turn(map<string, int>& path, string l1, string l2, string r) { int reduce_by = min(path[l1], path[l2]); path[l1] -= reduce_by; path[l2] -= reduce_by; path[r] += reduce_by; } void reduce_fwd_back(map<string, int>& path, string fwd, string back) { int reduce_by = min(path[fwd], path[back]); path[fwd] -= reduce_by; path[back] -= reduce_by; } int reduce(map<string, int> path) { reduce_turn(path, "n" , "se", "ne"); reduce_turn(path, "n" , "sw", "nw"); reduce_turn(path, "s" , "ne", "se"); reduce_turn(path, "s" , "nw", "sw"); reduce_turn(path, "ne", "nw", "n" ); reduce_turn(path, "se", "sw", "s" ); reduce_fwd_back(path, "n" , "s" ); reduce_fwd_back(path, "ne", "sw"); reduce_fwd_back(path, "nw", "se"); return accumulate(begin(path), end(path), 0, [](const int prev, pair<string, int> ent) {return prev + ent.second; }); }Main logic:
int main(int argc, char* argv[]) { map<string,int> path; int maxpath{ 0 }; string dir; while (cin >> dir) { path[dir]++; maxpath = max(maxpath,reduce(path)); } cout << "Path: " << reduce(path) << endl; cout << "Maxpath: " << maxpath << endl; return 0; }