Skip to content

Instantly share code, notes, and snippets.

@rafaelbm
Created April 28, 2020 01:31
Show Gist options
  • Save rafaelbm/908682935aa0d0fa69d0e049d4a1c844 to your computer and use it in GitHub Desktop.
Save rafaelbm/908682935aa0d0fa69d0e049d4a1c844 to your computer and use it in GitHub Desktop.
using FluentAssertions;
using Xunit;
namespace Challenges
{
public class Challenges
{
/*
Eight houses represented as cells, are arranged in a straight line. Each day every cell competes with its adjacent cells (neighbors)...
Integer value of 1 represents an active cell and a value of 0 represents an inactive cell. If the neighbors on both the sides of a cell are either active or inactive,
the cell becomes inactive on the next day otherwise the cell becomes active. The two cells on each end have a single adjacent cell, so assume that the unoccupied space
on the opposite side is an inactive cell. Even after updating the cell state consider its previous state when updating the state of other cells The state information of
all cells should be updated simultaneously Write en algorithm to output the state of the cells after the glven number of days Input The input to the function/method cons.
of two arguments state. a I ist of integers representing the current state of cells, clay, an Integer representIng the number of days
Output Return a list of integers representing the state of the cells after the given number of days
Note The elements of the list states contains Os and 5 only
*/
[Fact]
public void TestCaseOne()
{
int[] cels = new[] { 1, 0, 0, 0, 0, 1, 0, 0 };
int days = 1;
var response = MagicDoer.DoMagic(cels, days);
response.Should()
.BeEquivalentTo(new[] { 0, 1, 0, 0, 1, 0, 1, 0 });
}
[Fact]
public void TestCaseTwo()
{
int[] cels = new[] { 1, 1, 1, 0, 1, 1, 1, 1 };
int days = 2;
var response = MagicDoer.DoMagic(cels, days);
response.Should()
.BeEquivalentTo(new[] { 0, 0, 0, 0, 0, 1, 1, 0 });
}
}
public class MagicDoer
{
public class State
{
public const int Inactive = 0;
public const int Active = 1;
}
public static int[] DoMagic(int[] cells, int days)
{
var nextDayCells = new int[cells.Length];
for (int day = 0; day < days; day++)
{
for (int currentCellIndex = 0; currentCellIndex < cells.Length; currentCellIndex++)
{
var previousNeighborIndex = currentCellIndex - 1;
var nextNeighborIndex = currentCellIndex + 1;
var isCurrentCellFirstCell = previousNeighborIndex == -1;
var previousNeighborState = isCurrentCellFirstCell
? State.Inactive
: cells[previousNeighborIndex];
var isCurrentCellLastCell = nextNeighborIndex == cells.Length;
var nextNeighboorState = isCurrentCellLastCell
? State.Inactive
: cells[nextNeighborIndex];
var neighborsHaveSameState = previousNeighborState == nextNeighboorState;
nextDayCells[currentCellIndex] = neighborsHaveSameState
? State.Inactive
: State.Active;
}
// The day is setting, lets copy over our nextDayCells to cells so the algorithm can use the correct cell states for the next day.
nextDayCells.CopyTo(cells, index: 0);
}
return nextDayCells;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment