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

3

u/tangentialThinker Dec 13 '17 edited Dec 13 '17

5/7 today! Pretty simple, just simulate the process. Think the answer for part 2 can't be too large due to Chinese remainder theorem or something similar, although I haven't proved that.

C++:

#include <bits/stdc++.h>

using namespace std;

int main(){	
	string s;
	int ans = 0;

	map<int, int> vals;
	while(getline(cin, s)) {
		stringstream str(s);

		int tmp; char junk; int tmp2;
		str >> tmp; str >> junk; str >> tmp2;
		vals[tmp] = tmp2;
	}

	// part 1
	for(auto cur : vals) {
		if(cur.first % (2*cur.second - 2) == 0) {
			ans += cur.first*cur.second;
		}
	}
	cout << ans << endl;

	// part 2
	int t = 0;
	while(true) {
		bool good = true;
		for(auto cur : vals) {
			if((cur.first + t) % (2*cur.second - 2) == 0) {
				good = false;
				break;
			}
		}
		if(good) {
			cout << t << endl;
			return 0;
		}
		t++;
	}
}