using System.Collections.Generic;
public class WastedSandCounter
public int Count(int[,] sandPile)
Queue<(int i,int j)> positions = new Queue<(int i,int j)>();
for(int i=0;i< sandPile.GetLength(0); i++)
for(int j=0;j< sandPile.GetLength(1); j++)
positions.Enqueue((i,j));
while (positions.Count >0)
var(l,c) = positions.Dequeue();
sandPile[l,c] = sandPile[l,c] -4;
positions.Enqueue((l,c));
positions.Enqueue((l-1,c));
positions.Enqueue((l,c-1));
if(l+1< sandPile.GetLength(0)){
positions.Enqueue((l+1,c));
if(c+1< sandPile.GetLength(1)){
positions.Enqueue((l,c+1));
private readonly string[] _people;
private readonly string[] _productTypes;
private readonly int _initialInventory;
public Marketplace(string[] people, string[] productTypes, int initialInventory)
_productTypes = productTypes;
_initialInventory = initialInventory;
private int[,] inventory;
public int CheckInventory(IEnumerable<string> transactions, string person, string productType)
m = _productTypes.Length;
inventory = new int[n,m];
inventory[i,j]=_initialInventory;
int transactNo = transactions.Count();
seller = GetSellerIndex(transactions.ElementAt(i));
buyer = GetBuyerIndex(transactions.ElementAt(i));
String str=transactions.ElementAt(i);
for (int i = 0; i < str.Length; i++)
if (Char.IsDigit(str[i]))
amount = amount * 10 + (int)Char.GetNumericValue(str[i]);
for (int i = 0; i < _productTypes.Length; i++)
if (str.IndexOf(_productTypes[i]) != -1)
if(inventory[seller,product]-amount>=0){
inventory[seller,product] -= amount;
inventory[butyer,product] += amount;
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()