Skip to content

Instantly share code, notes, and snippets.

@jessenich
Last active September 28, 2020 14:35
Show Gist options
  • Select an option

  • Save jessenich/28684285486eb2fa8fc4e40107975819 to your computer and use it in GitHub Desktop.

Select an option

Save jessenich/28684285486eb2fa8fc4e40107975819 to your computer and use it in GitHub Desktop.
/*
* There is a colony of 8 cells arranged in a straight line where each day every cell competes with
* its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both
* inactive, the cell becomes inactive the next day, otherwise it becomes active the next day.
*
* Assumptions
* The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to
* be always inactive. Even after updating the cell state. consider its previous state for updating the
* state of other cells. Update the cell information of all cells simultaneously.
*
* Write a fuction cellCompete which takes takes one 8 element array of integers cells representing the
* current state of 8 cells and one integer days representing the number of days to simulate. An integer
* value of 1 represents an active cell and value of 0 represents an inactive cell.
*/
using System;
namespace AmazonAssessment
{
public static class CellComplete
{
public static void Run()
{
var states = new int[] { 1, 0, 0, 0, 0, 1, 0, 0 };
var output = Solution(states, 1);
for (int i = 0; i < output.Length; i++)
Console.Write($"{output[i]}, ");
}
public static int[] Solution(int[] states, int days)
{
const int farLeft = 0;
const int farRight = 0;
while (days-- != 0)
{
var endState = new int[states.Length];
for (int i = 0; i < states.Length; i++)
{
int left, right;
left = i == 0 ?
farLeft :
states[i - 1];
right = i == states.Length - 1 ?
farRight :
states[i + 1];
endState[i] = left == right ? 0 : 1;
}
states = endState;
}
return states;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment