using System.Collections.Generic;
public class WastedSandCounter
public int checkNeighbouringPos(int[,] sandPile, int i, int j)
int rows = sandPile.GetLength(0);
int columns = sandPile.GetLength(1);
while (sandPile[i, j] > 3)
grainsFallen += checkNeighbouringPos(sandPile, i - 1, j);
grainsFallen += checkNeighbouringPos(sandPile, i, j - 1);
grainsFallen += checkNeighbouringPos(sandPile, i, j + 1);
grainsFallen += checkNeighbouringPos(sandPile, i + 1, j);
public int Count(int[,] sandPile)
int rows = sandPile.GetLength(0);
int columns = sandPile.GetLength(1);
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
while (sandPile[i, j] > 3)
grainsFallen += checkNeighbouringPos(sandPile, i - 1, j);
grainsFallen += checkNeighbouringPos(sandPile, i, j - 1);
grainsFallen += checkNeighbouringPos(sandPile, i, j + 1);
grainsFallen += checkNeighbouringPos(sandPile, i + 1, j);
private readonly string[] _people;
private readonly string[] _productTypes;
private readonly int _initialInventory;
private Dictionary<string, Dictionary<string, int>> marketPlaceDataStructure;
public Marketplace(string[] people, string[] productTypes, int initialInventory)
_productTypes = productTypes;
_initialInventory = initialInventory;
marketPlaceDataStructure = new Dictionary<string, Dictionary<string, int>>();
void fillMarketStructure()
for(int i = 0 ; i < this._people.Length; i++)
if(!this.marketPlaceDataStructure.ContainsKey(this._people[i]))
this.marketPlaceDataStructure[this._people[i]] = new Dictionary<string, int>();
for(int j = 0 ; j < this._productTypes.Length; j++)
this.marketPlaceDataStructure[this._people[i]][this._productTypes[j]] = this._initialInventory;
public int CheckInventory(IEnumerable<string> transactions, string person, string productType)
this.fillMarketStructure();
foreach (string transaction in transactions)
string[] transactionSplit = transaction.Split(" ");
string firstName = transactionSplit[0];
string secondName = transactionSplit[transactionSplit.Length - 1];
string fruitName = transactionSplit[3];
string verb = transactionSplit[1];
int number = Int32.Parse(transactionSplit[2]);
if(this.marketPlaceDataStructure[firstName][fruitName] >= number)
this.marketPlaceDataStructure[firstName][fruitName] -= number;
this.marketPlaceDataStructure[secondName][fruitName] += number;
else if(verb.Equals("bought"))
if(this.marketPlaceDataStructure[secondName][fruitName] >= number)
this.marketPlaceDataStructure[secondName][fruitName] -= number;
this.marketPlaceDataStructure[firstName][fruitName] += number;
return this.marketPlaceDataStructure[person][productType];
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);
var counter = new WastedSandCounter();
counter.Count(table).ShouldBe(10);
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()