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!

40 Upvotes

426 comments sorted by

View all comments

1

u/[deleted] Dec 03 '18 edited Dec 03 '18

[Card] I'm ready for today's puzzle because I have the Savvy Programmer's Guide to C++ tuples and sets

C++

I think I did what most people did: Put every square inch in a suitable data structure, with a counter.

In C++, my choice was a map<tuple<int,int>, int> to have a way of counting claims for every square inch (tuple<int,int> being the two-dimensional coordinates).

Tuples are great for being used as keys in maps and in sets, but they lack readability. Let's see what other people did.

(see on github)

int main(){
	regex rex("#(\\d+) @ (\\d+),(\\d+): (\\d+)x(\\d+)");
	vector<tuple<int,int,int,int,int>> claims;
	map<tuple<int,int>, int> laps;

	/* 
	 * Read the input into
	 * * A set of tuple containing all information
	 * * A map of two-dimensional coordinates with an overlap counter
	 */
	for(string line;getline(cin, line);){
		smatch match;
		if(regex_match(line, match, rex)){
			int id = boost::lexical_cast<int>(match[1]);
			int posX = boost::lexical_cast<int>(match[2]);
			int posY = boost::lexical_cast<int>(match[3]);
			int width = boost::lexical_cast<int>(match[4]);
			int height = boost::lexical_cast<int>(match[5]);

			for(int x_offset=0;x_offset<width;x_offset++){
				for(int y_offset=0;y_offset<height;y_offset++){
					laps[{posX+x_offset, posY+y_offset}]++;
				}
			}
			claims.push_back({id, posX, posY, width, height});
		}
	}
	
	/*
	 * Count how many overlaping square inches we have
	 */
	cout << "Part 1: " 
	     << count_if(begin(laps), end(laps), [](auto pair){return pair.second > 1;})
	     << endl;

	/*
	 * Iterate over all claims, check every field for overlaps
	 */
	auto notOverlapped = find_if(claims.begin(), claims.end(), [&laps](auto &claim){
		int posX = get<1>(claim);
		int posY = get<2>(claim);
		int width = get<3>(claim);
		int height = get<4>(claim);

		for(int x_offset=0;x_offset<width;x_offset++){
			for(int y_offset=0;y_offset<height;y_offset++){
				if(laps[{posX+x_offset, posY+y_offset}] != 1){
					return false;
				}
			}
		}
		return true;
	});

	cout << "Part 2: "
	     << get<0>(*notOverlapped) 
	     << endl;

	return 0;
}

2

u/willkill07 Dec 04 '18

In C++17 you could do the following:

auto [posX, poxY, width, height] = claim;

That makes tuples much more readable. You could also just create a simple struct

1

u/[deleted] Dec 04 '18

You mean, instead of std::get? Cool, thanksl! But I omitted the first element of the tuple?

2

u/willkill07 Dec 04 '18

You can just add _, to the beginning of the capture to β€œignore” the first

1

u/[deleted] Dec 04 '18

I like it, thank you!