Unable to load image

Day 18 AoC: A nice break :marseybeanrelieved:

This one was very easy compared to the last two days

10
Jump in the discussion.

No email address required.

Pretty easy today. Part one as you would do it and part two as a bfs that will stop if it traverse into a cubes and counts +1 to the counter of sides facing the water.


#include <iostream>
#include <tuple>
#include <set>
#include <string>
#include <fstream>
#include <sstream>
#include <format>
#include <vector>
#include <queue>
#include <map>

struct coordinate {
	int x;
	int y;
	int z;

	coordinate operator+(const coordinate r) const {
		return {x + r.x, y + r.y, z + r.z};
	}

	auto operator<=>(const coordinate&) const = default;
};

typedef std::set<coordinate> set_type;

std::vector<coordinate> dirs{ { {0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0} } };

int count_neigbors(coordinate coord, const set_type& set) {
	int count{ 0 };
	for (auto dir : dirs) {
		if (set.contains(coord + dir)) count++;
	}
	return count;
}

int main()
{
	std::fstream file("input.txt");
	std::string buf;

	set_type cubes;

	int max_dimension{ INT_MIN };
	int min_dimension{ INT_MAX };

	while (std::getline(file, buf)) {
		int x, y, z;
		char c;
		std::istringstream iss( buf );
		iss >> x >> c >> y >> c >> z;
		max_dimension = std::max({ x, y, z, max_dimension });
		min_dimension = std::min({ x, y, z, min_dimension });
		cubes.insert({x,y,z});
	}
	int total{0};
	for (auto it = cubes.cbegin(); it != cubes.cend(); ++it) {
		total += 6 - count_neigbors(*it, cubes);
	}
	std::cout << "Part One: " << total << '\n';

	// We need to be able to "walk" around the entire cube area
	++max_dimension;
	--min_dimension;

	set_type explored;
	std::queue<coordinate> q;
	int result{ 0 };
	
	coordinate start { 0, 0, 0 };
	q.push(start);

	while (!q.empty()) {
		auto& cur = q.front();
		q.pop();
		if (explored.find(cur) != explored.end()) continue;
		explored.insert(cur);
		for (const auto& dir : dirs) {
			coordinate next = cur + dir;
			if (next.x > max_dimension || next.y > max_dimension || next.z > max_dimension ||
				next.x < min_dimension || next.y < min_dimension || next.z < min_dimension)
				continue;
			if (cubes.contains(next)) {
				result += 1;
			}
			else {
				q.push(next);
			}
		}
	} 
	std::cout << "Part Two " << result << '\n';
}

Jump in the discussion.

No email address required.

I don't have enough spoons to read this shit

Jump in the discussion.

No email address required.

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