Unable to load image

Advent of Code day 6

was preparing my butthole after the last one but ended up being the fastest one i've done :marseyexcited:

with open("day6input.txt") as f:
    input = f.read()

# part 1

for i in range(len(input)):
    marker = set(input[i:i+4])
    if len(marker) == 4:
        answer = i + 4
        break

print(answer)

# part 2 

for i in range(len(input)):
    marker = set(input[i:i+14])
    if len(marker) == 14:
        answer = i + 14
        break

print(answer)
28
Jump in the discussion.

No email address required.

c++

spent some time to find a O(1) for adding and checking if string is unique and then just gave up and ended up on O(n), with n being the length of the substring


bool isUnique(const std::string& substring) {
	std::array<bool, 26> chars{false};

	for (std::size_t i{0 }; i < substring.size(); ++i) {
		if (chars[substring[i] - 'a']) return false;
		chars[substring[i] - 'a'] = true;
	}
	return true;
}


int solution(const std::string& buf, int word_size) {
	std::string substring;
	std::size_t idx{ 0 };

	for (std::size_t i{ 0 }; i < buf.size(); ++i) {
		if (substring.size() < word_size) substring += buf[i];
		if (substring.size() == word_size && isUnique(substring)) {
			idx = i;
			break;
		}
		else {
			substring.erase(0, 1);
		}
		substring += buf[i];
	}
	return idx;
}

int main() {
	std::ifstream file("input.txt");

	std::string buf;
	std::getline(file, buf);
	auto a = std::chrono::high_resolution_clock::now();
	int p1 = solution(buf, 4);
	auto b = std::chrono::high_resolution_clock::now();
	int p2 = solution(buf, 14);
	auto c = std::chrono::high_resolution_clock::now();


	auto ab = std::chrono::duration_cast<std::chrono::microseconds>(b - a);

	auto bc = std::chrono::duration_cast<std::chrono::microseconds>(c - b);

	std::cout << "Part One: " << p1 << " Time: " << ab << '\n'
		<< "Part Two: " << p2 << " Time: " << bc << '\n';
}

Jump in the discussion.

No email address required.

Sorry ma'am, looks like his delusions have gotten worse. We'll have to admit him.

Jump in the discussion.

No email address required.

Okay here is the O(1) version for checking if string is unique



int solution(const std::string& buf, int word_size) {
	std::string substring;
	std::size_t idx{ 0 };

	std::array<int, 26> uniqueChars{ 0 }; 

	int distinct_count{ 0 }; 

	for (std::size_t i{ 0 }; i < word_size; ++i) { 
		if (uniqueChars[buf[i] - 'a'] == 0) ++distinct_count;
		++uniqueChars[buf[i] - 'a']; 
	}

	for (std::size_t i = word_size ; i < buf.size(); ++i) {
		if (uniqueChars[buf[i - word_size] - 'a'] == 1) { 
			--distinct_count;							 
		}

		--uniqueChars[buf[i - word_size] - 'a'];

		if (uniqueChars[buf[i] - 'a'] == 0) ++distinct_count;

		++uniqueChars[buf[i] - 'a'];

		if (distinct_count == word_size) {
			idx = i + 1;
			break;
		}
	}
	return idx;
}

int main() {
	std::ifstream file("input.txt");

	std::string buf;
	std::getline(file, buf);
	auto a = std::chrono::high_resolution_clock::now();
	int p1 = solution(buf, 4);
	auto b = std::chrono::high_resolution_clock::now();
	int p2 = solution(buf, 14);
	auto c = std::chrono::high_resolution_clock::now();


	auto ab = std::chrono::duration_cast<std::chrono::microseconds>(b - a);

	auto bc = std::chrono::duration_cast<std::chrono::microseconds>(c - b);

	std::cout << "Part One: " << p1 << " Time: " << ab << '\n'
		<< "Part Two: " << p2 << " Time: " << bc << '\n';
}

The first version ran in(without reading from the file):
Part One: 1109 Time: 24us
Part Two: 3965 Time: 62us

The second version in:
Part One: 1109 Time: 3us
Part Two: 3965 Time: 8us

Jump in the discussion.

No email address required.

No, don't reply like this, please do another wall of unhinged rant please.

Jump in the discussion.

No email address required.

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