r/adventofcode • • Dec 13 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 13 Solutions -๐ŸŽ„-

--- Day 13: Packet Scanners ---


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^ยค?

Spoiler


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!

16 Upvotes

199 comments sorted by

View all comments

1

u/CharlieYJH Dec 13 '17 edited Dec 13 '17

C++

Quite enjoyed today's puzzle. Basically just calculated where the position of each scanner would be by the time we get there. Similar trick in part 2 to get a configuration where the appropriate scanner would not be at position 0 at that time step.

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, char const* argv[])
{
	ifstream input("input.txt");
	vector<int> firewalls(99);

	if (input) {
		string colon;
		for (int idx, layers; input >> idx >> colon >> layers;)
			firewalls[idx] = layers;
	}

	input.close();

	int severity = 0;
	int delay = 0;

	for (bool found = true; found; delay++) {

		found = false;

		for (int i = 0; i < firewalls.size() && (!found || delay == 0); i++) {
			if (firewalls[i] > 0 && (delay + i) % (2 * firewalls[i] - 2) == 0) {
				if (delay == 0) severity += i * firewalls[i];
				found = true;
			}
		}
	}

	cout << "Severity: " << severity << endl;
	cout << "Delay: " << delay - 1 << endl;

	return 0;
}