using System.Collections.Generic;
public class WastedSandCounter
private (int, int) Position(int[,] sandPile)
for (i=0;i<sandPile.GetLength(0);i++)
for (j=0;j<sandPile.GetLength(1);j++)
private bool ExistMoreThan3Grains(int[,] sandPile)
for (int i=0;i<sandPile.GetLength(0);i++)
for (int j=0;j<sandPile.GetLength(1);j++)
public int Count(int[,] sandPile)
while(ExistMoreThan3Grains(sandPile))
(int i,int j) position = Position(sandPile);
if(position.i==0 && position.j==0)
sandPile[position.i,position.j] -= 4;
else if(position.i==0 && position.j==sandPile.GetLength(1)-1)
sandPile[0,sandPile.GetLength(1)-2]++;
sandPile[1,sandPile.GetLength(1)-1]++;
sandPile[position.i,position.j] -= 4;
sandPile[0,position.j-1]++;
sandPile[0,position.j+1]++;
sandPile[1,position.j]++;
sandPile[position.i,position.j] -= 4;
else if(position.i==sandPile.GetLength(0)-1 && position.j==0)
sandPile[sandPile.GetLength(0)-2,0]++;
sandPile[sandPile.GetLength(0)-1,1]++;
sandPile[position.i,position.j] -= 4;
else if(position.i== sandPile.GetLength(0)-1 && position.j==sandPile.GetLength(1)-1)
sandPile[sandPile.GetLength(0)-2,sandPile.GetLength(1)-1]++;
sandPile[sandPile.GetLength(0)-1,sandPile.GetLength(1)-2]++;
sandPile[position.i,position.j] -= 4;
else if(position.i== sandPile.GetLength(0)-1)
sandPile[position.i,position.j-1]++;
sandPile[position.i,position.j+1]++;
sandPile[position.i-1,position.j]++;
sandPile[position.i,position.j] -= 4;
sandPile[position.i-1,0]++;
sandPile[position.i+1,0]++;
sandPile[position.i,1]++;
sandPile[position.i,position.j] -= 4;
else if(position.j==sandPile.GetLength(1)-1)
sandPile[position.i-1,position.j]++;
sandPile[position.i+1,position.j]++;
sandPile[position.i,position.j-1]++;
sandPile[position.i,position.j] -= 4;
sandPile[position.i,position.j-1]++;
sandPile[position.i,position.j+1]++;
sandPile[position.i-1,position.j]++;
sandPile[position.i+1,position.j]++;
sandPile[position.i,position.j] -= 4;
private readonly string[] _people;
private readonly string[] _productTypes;
private readonly int _initialInventory;
public Marketplace(string[] people, string[] productTypes, int initialInventory)
_productTypes = productTypes;
_initialInventory = initialInventory;
public int CheckInventory(IEnumerable<string> transactions, string person, string productType)
throw new NotImplementedException();
public static void Main()
private static void Run()
new WastedSandCounterTests().Start();
new MarketplaceTests().Start();
public class WastedSandCounterTests
Tests.Run("WastedSandCounter should", (it) =>
it.Should(NotCountSandFallingOnTheTable,
"not count sand that falls on the table");
it.Should(CountAllSandFallingOffTheTable,
"count sand that falls off the table");
it.Should(FullyCollapseTallSandPiles,
"fully collapse tall piles of the sand");
public void NotCountSandFallingOnTheTable()
var counter = new WastedSandCounter();
counter.Count(table).ShouldBe(0);
public void CountAllSandFallingOffTheTable()
var counter = new WastedSandCounter();
counter.Count(table).ShouldBe(12);
public void FullyCollapseTallSandPiles()
var counter = new WastedSandCounter();
counter.Count(table).ShouldBe(6);
public class MarketplaceTests
Tests.Run("Marketplace should", (it) =>
it.Should(() => NotDoInvalidTransactions(), "not do transactions that are invalid");
it.Should(() => DoValidTransactions(), "do transactions that are invalid");
public void NotDoInvalidTransactions()
var marketplace = new Marketplace(new []{"Ana", "Ion"}, new []{"apples"}, 3);
marketplace.CheckInventory(new[] {"Ana sold 4 apples to Ion"}, "Ana", "apples").ShouldBe(3);
marketplace.CheckInventory(new[] {"Ion bought 4 apples from Ana"}, "Ana", "apples").ShouldBe(3);
public void DoValidTransactions()
var marketplace = new Marketplace(new []{"Ana", "Ion"}, new []{"apples"}, 4);
marketplace.CheckInventory(new[] {"Ana sold 4 apples to Ion", "Ion sold 2 apples to Ana"}, "Ana", "apples").ShouldBe(2);
public static void Run(string testGroup, Action<Tests> tests)
public static void StartTests(string testGroup)
Console.WriteLine("====================================================");
Console.WriteLine(testGroup);
Console.WriteLine("====================================================");
public void Should(Action test, string testName)
Console.WriteLine(testName);
Console.WriteLine(" Passed");
Console.WriteLine(" Failed");
Console.WriteLine(" " + e.Message);
public static void EndTests()