// Freehold High School

// Nolan Lum

// Contest #3

// Senior Division

// ACSL Mancala

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Mancala

{

      public class Program

      {

            public static void Main(string[] args)

            {

                  int[] state = {0, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4};

 

                  for (int i = 0; i < 5; i++)

                  {

                        Console.Write("Input: ");

                        var data = (from x in Console.ReadLine().Replace(" ", "").ToUpper().Split(',')

                                       select NumToIdx(x)).ToArray();

 

                        Move(state, data[0], i % 2 == 0 ? 1 : 0);

                        Console.WriteLine(state[data[1]]);

                  }

 

                  Console.Write("Press any key to continue...");

                  Console.ReadKey(true);

            }

 

            private static int NumToIdx(string str) { return char.IsDigit(str, 0) ? int.Parse(str) < 7 ? int.Parse(str) : int.Parse(str) + 1 : str.ToUpper() == "A" ? 7 : 0; }

            private static bool IsMancala(int player, int index) { return index == player * 7; }

            private static int Negate(int player) { return player == 1 ? 0 : 1; }

            private static int AddMod14(int val, int val2) { return (val + val2) % 14; }

            private static int IncMod14(int val) { return (val + 1) % 14; }

 

            private static void Move(int[] state, int start, int player)

            {

                  int num = state[start], fin = 0;

                  state[start] = 0;

 

                  for (int i = 0, j = IncMod14(start); i < num; i++, j = IncMod14(j))

                  {

                        if (IsMancala(Negate(player), j)) j = IncMod14(j);

 

                        state[j]++;

                        fin = j;

                  }

 

                  if (!(state[fin] == 1 || state[fin] == 4 || IsMancala(player, fin)))

                        Move(state, fin, player);

            }

      }

}