I'm trying to solve this too but in C++. I'm not sure what I'm doing wrong, the code seemed so easy... Please help.
EDIT: Fixed the mixed-up loop indexes (inside a for loop over the variable i I had other for loops over the variable i. That'd be bad) but still WA...
[cpp]
using namespace std;
#include <iostream>
#include <string>
#include <sstream>
const int DNA_SIZE = 10;
const int DISH_SIZE = 40;
const int FIRST_DISH = 20;
const int DAYS = 50;
int program[DNA_SIZE];
int dish[DISH_SIZE];
int new_dish[DISH_SIZE]; // temporary holder for next day's values
int main()
{
string in_line;
while (getline(cin, in_line))
{
istringstream input(in_line);
// Initialize the dish
for (int i = 0; i < DISH_SIZE; i++)
{
if (i == FIRST_DISH - 1) dish
= 1;
else dish = 0;
new_dish = 0;
}
// Read in the program
for (int i = 0; i < DNA_SIZE; i++) input >> program;
// Run the simulation
for (int i = 0; i < DAYS; i++)
{
// Write today's values from the dish
for (int j = 0; j < DISH_SIZE; j++)
{
switch (dish[j])
{
case 0: cout << ' '; break;
case 1: cout << '.'; break;
case 2: cout << 'x'; break;
case 3: cout << 'W'; break;
}
}
cout << '\n';
// Handle the left end first, it is special.
int left_sum = dish[0] + dish[1];
new_dish[0] = program[left_sum];
for (int j = 1; j < DISH_SIZE - 1; j++)
{
int sum = dish[j - 1] + dish[j] + dish[j + 1];
new_dish[j] = program[sum];
}
// Handle the right end last, it is special.
int right_sum = dish[DISH_SIZE - 1] + dish[DISH_SIZE - 2];
new_dish[DISH_SIZE - 1] = program[right_sum];
// Copy tomorrow's values into the dish
for (int j = 0; j < DISH_SIZE; j++) dish[j] = new_dish[j];
}
cout << '\n';
}
}
[/cpp]