Unable to load image

Advent of code day 4 :marseysad:

This is the advent of code day 4 thread for bragging about your solution and bullying people who haven't gotten it yet :marseyill:

35
Jump in the discussion.

No email address required.

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

struct Range
{
	long low;
	long upper;

	bool inline isContainedIn(const struct Range &larger) const
	{
		return larger.low <= low and larger.upper >= upper;
	}

	bool inline isIntersectionEmptySet(const struct Range &r) const
	{
		return upper < r.low or low > r.upper;
	}
};

// G-d forgive me bc I used C++ Slowpoke regex
std::regex ranges_reg("(\\d+)\\-(\\d+),(\\d+)\\-(\\d+)");

int main()
{
	std::string line;
	unsigned card_pairs = 0;

	while(getline(std::cin, line))
	{
		std::smatch match;
		std::regex_search(line, match, ranges_reg);

		Range elf1 = {std::stol(match.str(1)), std::stol(match.str(2))};
		Range elf2 = {std::stol(match.str(3)), std::stol(match.str(4))};

		// PART A
		//if(elf1.isContainedIn(elf2) or elf2.isContainedIn(elf1))
		//	card_pairs ++;

		// PART B
		if(not elf1.isIntersectionEmptySet(elf2))
			card_pairs ++;
	}

	std::cout << card_pairs << std::endl;
}
Jump in the discussion.

No email address required.

Ranges

Regex

:marseysick::marseysick::marseysick:

Jump in the discussion.

No email address required.

Very nice, let's see your implementation

![](/images/16701585682228355.webp)

Jump in the discussion.

No email address required.

felt to unmotivated to join this contest thing :marseygiveup:

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.