r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

1

u/[deleted] Dec 03 '18

C++ ResidentSleeper:

    // rishav.io

#include <iostream>
#include <string>
#include <regex>
#include <set>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

struct S {
	int id, left, top, width, height;
};

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);

#ifdef __APPLE__
	freopen("input.txt", "r", stdin);
#endif

	string s;
	regex rg(R"(#(\d+) @ (\d+),(\d+): (\d+)x(\d+))", std::regex::optimize);
	smatch pieces_match;

	set<PII> one, two;

	vector<S> vec;

	while (getline(cin, s)) {
		if (regex_match(s, pieces_match, rg)) {
			int id = stoi(pieces_match[1].str());
			int left = stoi(pieces_match[2].str());
			int top = stoi(pieces_match[3].str());
			int width = stoi(pieces_match[4].str());
			int height = stoi(pieces_match[5].str());

			vec.push_back(S{id, left, top, width, height});

			for (int i = top; i < top + height; i++) {
				for (int j = left; j < left + width; j++) {
					PII x = {i, j};
					if (one.find(x) == one.end()) {
						one.insert(x);
					} else {
						one.erase(x);
						two.insert(x);
					}
				}
			}
		}
	}

	for (auto &p : vec) {
		bool f = true;
		for (int i = p.top; (i < p.top + p.height) && f; ++i) {
			for (int j = p.left; (j < p.left + p.width) && f; ++j) {
				if (two.find({i, j}) != two.end()) f = false;
			}
		}
		if (f) cout << p.id << '\n';
	}

	cout << two.size() << '\n';

}