Example #1
0
int searchInput(const Program& p)
{
	int inputValue = 0;
	int time = 0;
	const int maxIterations = 1000;

	bool invalid=false;
	while(true)
	{
		Computer c;
		c.program = p;
		c.setOut([&c, &invalid, &time, maxIterations](int v)
		{
			if ( (time%2) != v)
			{
				// incorrect out
				invalid = true;
				c.halt();
			}
			if (time == maxIterations)
			{
				c.halt();
			}
			time++;
		});
		c.reset();
		invalid = false;
		time = 0;
		c.registers.registerValues[0] = inputValue;
		c.start();
		if(!invalid)
		{
			// yes!!!
			return inputValue;
		}

		inputValue++;
	}
	return -1;
}