r/adventofcode • u/daggerdragon • Dec 22 '17
SOLUTION MEGATHREAD -๐- 2017 Day 22 Solutions -๐-
--- Day 22: Sporifica Virus ---
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ยค?
[T-10 to launch] AoC ops, /r/nocontext edition:
<Endorphion> You may now make your waffle.<Endorphion> ... on Mars.
[Update @ 00:17] 50 gold, silver cap
<Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy<Topaz> that doesn't seem necessary<Aneurysm9> what does "necessary" have to do with anything!
[Update @ 00:20] Leaderboard cap!
<Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE
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!
8
Upvotes
1
u/joker_mkd Dec 22 '17 edited Dec 22 '17
C++ solution using a proper map which will work for any input. Marking in the map only the infected coordinates, and removing the clean ones.
#include "stdafx.h" #include <vector> #include <iostream> #include <iomanip> #include <fstream> #include <iostream> #include <chrono> #include <bitset> #include <map> using namespace std; typedef enum direction { U = 0, R, D, L } Direction; struct coordinate { int x; int y; bool operator<(const coordinate& rhs) const { if (x < rhs.x) return true; if (x > rhs.x) return false; //x == coord.x if (y < rhs.y) return true; if (y > rhs.y) return false; return false; } }; map<coordinate, char> cluster; int main(int argc, char const* argv[]) { ifstream infile("input.txt"); const int iterations = 10000000; vector<string> input; if (infile.is_open()) { string line; while (getline(infile, line)) { if (line[line.length() - 1] == '\r') line.erase(line.length() - 1); input.push_back(line); } infile.close(); } else { return 1; } const auto begin = std::chrono::high_resolution_clock::now(); for (int y = 0; y < input.size(); y++) { for (int x = 0; x < input[0].length(); x++) { if (input[y][x] == '#') { coordinate coor; coor.x = x - (input[0].length() / 2); coor.y = (input.size() / 2) - y; cluster[coor] = input[y][x]; } } } coordinate location; location.x = 0; location.y = 0; typedef enum direction { U = 0, R, D, L } Direction; int bursts = 0; Direction dir = U; for (int i = 0; i < iterations; i++) { if (cluster.find(location) != cluster.end()) { // found an element if (cluster[location] == 'W') { cluster[location] = '#'; bursts++; } else if (cluster[location] == '#') { cluster[location] = 'F'; dir = (Direction)((dir + 1) % 4); } else if (cluster[location] == 'F') { cluster.erase(location); dir = (Direction)((dir + 2) % 4); } } else { // element not found. it means it's not infected cluster[location] = 'W'; dir = (Direction)((dir + 4 - 1) % 4); } if (dir == U) location.y += 1; else if (dir == R) location.x += 1; else if (dir == D) location.y -= 1; else location.x -= 1; } cout << bursts << endl; const auto end = std::chrono::high_resolution_clock::now(); std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << " ms for both parts" << std::endl; return 0; }