Unable to load image

Advent of Code Day 5, AKA Stacks 101 :marseyinabox:

I actually stayed up to get a semi-decent score for once (being a eurocel for this is suffering). How are you all faring with shifting boxes around?

![](/images/16702184438592093.webp)

26
Jump in the discussion.

No email address required.


constexpr int stack_count{ 9 };


template<std::size_t SIZE>
void reverseStacks(std::array<std::stack<char>, SIZE>& stacks) {
	for (std::size_t i{ 0 }; i < stacks.size(); ++i) {
		std::stack<char> temp;

		while (!stacks[i].empty()) {
			temp.push(stacks[i].top());
			stacks[i].pop();
		}

		stacks[i].swap(temp);

	}
}

template<std::size_t SIZE>
void movePartOne(int count, int from, int to, std::array<std::stack<char>, SIZE>& stacks) {

	for (int i{ 0 }; i < count; ++i) {
		char temp = stacks[from - 1].top();
		stacks[from - 1].pop();
		stacks[to - 1].push(temp);
	}
}


template<std::size_t SIZE>
void movePartTwo(int count, int from, int to, std::array<std::stack<char>, SIZE>& stacks) {

	std::stack<char> temp;
	for (int i{ 0 }; i < count; ++i) {
		temp.push(stacks[from - 1].top());
		stacks[from - 1].pop();
	}

	while (!temp.empty()) {
		stacks[to - 1].push(temp.top());
		temp.pop();
	}
}


std::string solution(void (*moveCall)(int, int, int, std::array<std::stack<char>, stack_count>&)) {
	std::array<std::stack<char>, stack_count> crate_stacks;


	std::ifstream file("input.txt");

	std::string buf;

	while (std::getline(file, buf)) {
		if (buf[1] == '1') break;

		for (int i{ 0 }; i < stack_count; ++i) {
			char c = buf[4 * i + 1];
			if (c != ' ') {
				crate_stacks[i].push(c);
			}
		}
	}
	reverseStacks(crate_stacks);
	std::getline(file, buf);
	while (std::getline(file, buf)) {
		int count, from, to;
		std::string temp;
		std::istringstream iss(buf);
		iss >> temp >> count >> temp >> from >> temp >> to;
		moveCall(count, from, to, crate_stacks);
	}

	std::string out;

	for (std::size_t i{ 0 }; i < crate_stacks.size(); ++i) {
		out += crate_stacks[i].top();
	}
	return out;
}



int main() {
	std::string p1 = solution(movePartOne);
	std::string p2 = solution(movePartTwo);
	std::cout << "Part One: " << p1 << '\n'
		<< "Part Two: " << p2 << '\n';
}

Spent way to long on my decision to actually use real stacks, and couldn't figure out how to pass template<std::size_t SIZE> void movePartOne(int count, int from, int to, std::array<std::stack<char>, SIZE>& stacks) { as an argument to the solution-function, without hardcoding SIZE in solution, tbf not quite sure why i even wanted to use std::array now.

Jump in the discussion.

No email address required.

Impressive. Normally people with such severe developmental disabilities struggle to write much more than a sentence or two. He really has exceded our expectations for the writing portion. Sadly the coherency of his writing, along with his abilities in the social skills and reading portions, are far behind his peers with similar disabilities.

Jump in the discussion.

No email address required.

LPB is just WRECKING people on these challenges (including me).

Jump in the discussion.

No email address required.

:marseypearlclutch2:

Jump in the discussion.

No email address required.



Now playing: DK's Treehouse (DK64).mp3

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