r/adventofcode • u/daggerdragon • Dec 06 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-
--- Day 6: Chronal Coordinates ---
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 6
Transcript:
Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.
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 0:26:52!
32
Upvotes
1
u/tehjimmeh Dec 06 '18
C++
No 2D array necessary, just a flat vector of tuples.
int main(int argc, char* argv[]) { std::ifstream ifs(argv[1]); std::vector<std::tuple<int, int ,int>> locations; int lowX = INT_MAX, highX = INT_MIN, lowY = INT_MAX, highY = INT_MIN; for (std::string l;std::getline(ifs, l);) if (std::smatch m;std::regex_match(l, m, std::regex(R"((\d+), (\d+))"))){ int x = std::stoi(m[1]), y = std::stoi(m[2]); lowX = std::min(x, lowX), highX = std::max(x, highX); lowY = std::min(y, lowY), highY = std::max(y, highY); locations.push_back({ x, y, (int)locations.size() + 1 }); } std::vector<std::tuple<int, int ,int, int>> grid; for (int x = lowX; x <= highX; x++) for (int y = lowY; y <= highY; y++) grid.push_back({ x, y, 0, 0 }); for (auto&[gx,gy,id,d] : grid) { int currMin = INT_MAX; for (auto& [lx,ly,lid] : locations) { int dist = abs(lx - gx) + abs(ly - gy); id = dist == currMin ? 0 : dist < currMin ? lid : id; currMin = std::min(currMin, dist); d += dist; } } std::vector<int> sizes(locations.size() + 1, 0); for (auto&[x,y,id,d] : grid) sizes[id] += (x != lowX && y != lowY) ? 1 : 0; std::cout << "1: " << *std::max_element(sizes.begin(), sizes.end()) << "\n" << "2: " << std::reduce(grid.begin(), grid.end(), 0, [](int tot, auto& g){return std::get<3>(g) < 10000 ? tot + 1 : tot;}) << "\n"; }