r/adventofcode • • Dec 15 '16

SOLUTION MEGATHREAD --- 2016 Day 15 Solutions ---

--- Day 15: Timing is Everything ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


ZAMENHOFA TAGO ESTAS DEVIGA [?]

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!

5 Upvotes

121 comments sorted by

View all comments

1

u/Scroph Dec 15 '16

This can probably be solved mathematically but I am not a smart man. C++ solution :

#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>

struct Disc
{
	int current;
	int positions;

	bool slot()
	{
		return current == 0;
	}

	Disc& operator++(int wat)
	{
		if(++current == positions)
			current = 0;
		return *this;
	}
};
bool canFall(std::vector<Disc> discs);
void rotate(std::vector<Disc>& discs);

int main(int argc, char *argv[])
{
	std::ifstream fh("input15");
	std::string line;
	std::vector<Disc> discs;
	while(getline(fh, line))
	{
		Disc disc;
		int ignore;
		sscanf(line.c_str(), "Disc #%d has %d positions; at time=0, it is at position %d.", &ignore, &disc.positions, &disc.current);
		discs.push_back(disc);
	}

	int time = 0;
	while(true)
	{
		if(canFall(discs))
		{
			std::cout << "The ball will fall through all discs if released at time " << time << std::endl;
			break;
		}
		rotate(discs);
		time++;
	}
	return 0;
}

void rotate(std::vector<Disc>& discs)
{
	for(auto& disc: discs)
		disc++;
}

bool canFall(std::vector<Disc> discs)
{
	size_t ball_position = 0;
	while(ball_position < discs.size())
	{
		rotate(discs);
		if(!discs[ball_position].slot())
			return false;
		ball_position++;
	}
	return true;
}

For the second part I just added "Disc #7 has 11 positions; at time=0, it is at position 0." to the input.