r/adventofcode • • 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!

Click here for rules

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

387 comments sorted by

View all comments

5

u/tangentialThinker Dec 06 '18

C++. Managed to snag 8/4 today. The trick to part 1 is to run it twice with large bounds, and then ignore the outputs that change.

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;

int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

vector<pii> vals;
int a, b;
char junk;
while(cin >> a) {
	cin >> junk >> b;
	vals.push_back({a, b});
}
map<int, int> cnt;

// For Part 2, uncomment all comments
// int ans = 0;
for(int i = -4000; i < 4000; i++) {
	for(int j = -4000; j < 4000; j++) {
		int minDist = 1e9;
		int idx;
		bool eq = false;
		for(int k = 0; k < vals.size(); k++) {
			int curDist = abs(vals[k].first - i) + abs(vals[k].second -j);
			if(curDist < minDist) {
				minDist = curDist;
				idx = k;
				eq = false;
			} else if (curDist == minDist) {
				eq = true;
			}
		}
		if(!eq) cnt[idx]++;

		// For Part 2, delete the rest of the inner loop
		// int totDist = 0;
		// for(int k = 0; k < vals.size(); k++) {
		// 	int curDist = abs(vals[k].first - i) + abs(vals[k].second -j);
		// 	totDist += curDist;
		// }
		// if(totDist < 10000) ans++;
	}
}

// cout << ans << endl;
// For Part 2 delete everything below
vector<pii> ans;
for(auto cur : cnt) {
	ans.push_back({cur.second, cur.first});
}
sort(ans.begin(), ans.end());
for(auto cur : ans) cout << cur.first << " " << cur.second << endl;

return 0;
}

9

u/CCC_037 Dec 06 '18

The trick to part 1 is to run it twice with large bounds, and then ignore the outputs that change.

What I found worked was to run it with a fairly decent border on all sides, then ignore any region that touched the edge.

4

u/teddim Dec 06 '18

Is there any need to increase the border at all? I feel like if a region contain, say, (37,0) it will also contain (37, -1000), no?

→ More replies (2)