//Jeffrey Dudek

//Cary Academy

//ACSL1 SR-3 Division

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ACSL

{

    class Contest1

    {

        //Created by Jeffrey Dudek for ACSL Contest #1

        static void Main(string[] args)

        {

            //set up par

            int[] par_holes = new int[] { 3, 4, 5, 4, 4, 4, 5, 3, 4 };

            int par = 0;

            foreach (int i in par_holes)

                par += i;

 

            //used to calculate median score

            List<int> allscores;

 

            //used to hold scores

            int[,] scores;

 

            //used to hold the total score of the players

            player[] players;

 

            //the amount of wins for each player {A, B, C, D}

            int[] wins;

 

            while (true)

            {

                Console.WriteLine("Input:");

 

                //input scores

                scores = new int[4, 9];

                for (int i = 0; i < 9; i++) //take inputs

                {

                    string[] input = Console.ReadLine().Split(',');

                   

                    if (input.Count() < 4)

                    {

                        Console.WriteLine("ERROR: NOT ENOUGH ARGUMENTS");

                        Console.WriteLine("PLEASE ENTER AGAIN");

                        i--;

                        continue;

                    }

 

                    for (int j = 0; j < 4; j++)

                        if (!Int32.TryParse(input[j], out scores[j, i]))

                        {

                            Console.WriteLine("ERROR: WRITING SCORE: " + j + 1);

                            Console.WriteLine("PLEASE ENTER AGAIN");

                            i--;

                            break;

                        }

                }

 

                Console.WriteLine();

 

                //sets up the players total score

                players = new player[4];

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

                {

                    players[i].total_score = 0;

                    for (int j = 0; j < 9; j++)

                        players[i].total_score += scores[i, j];

                }

 

                //compares the total score of A and B to par

                Console.WriteLine(parScore(players[1].total_score, par));

                Console.WriteLine(parScore(players[0].total_score, par));

 

                //the amount of wins for each player {A, B, C, D}

                wins = new int[4];

                int won;

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

                {

                    won = GetWinner(scores, i);

                    if(won < 5)

                        wins[won]++;

                }

 

                //gets the player with the highest score

                int a = 0;

                for (int i = 1; i < 4; i++)

                    if (players[i].total_score < players[a].total_score)

                        a = i;

                //write their wins to the console

                Console.WriteLine(wins[a]);

 

                //set up the names of each of the players

                players[0].playertitle = "A";

                players[1].playertitle = "B";

                players[2].playertitle = "C";

                players[3].playertitle = "D";

 

                //sort players by score using a simple bubble sort

                player p;

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

                    try

                    {

                        if (players[i + 1].total_score < players[i].total_score)

                        {

                            p = players[i];

                            players[i] = players[i + 1];

                            players[i + 1] = p;

 

                            if (i > 0)

                                i--;

                            i--;

                        }

                    }

                    catch { }

 

                //writes the players in order using the sorted score

                Console.WriteLine("{0}, {1}, {2}, {3}", players[0].playertitle, players[1].playertitle, players[2].playertitle, players[3].playertitle);

 

                //creates a list of all the scores and sorts it

                allscores = new List<int>();

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

                    for (int j = 0; j < 9; j++)

                        allscores.Add(scores[i, j]);

                allscores.Sort();

 

                //write the median score (average of score[17] and score[18])

                Console.WriteLine((double)(allscores[17] + allscores[18]) / 2d);

 

                Console.WriteLine("\n");

            }

        }

 

        struct player

        { public int total_score; public string playertitle;};

 

        static string parScore(int score, int par)

        {

            if (score == par)

                return "par";

            else if (score < par)

                return (par - score) + " under par";

            return (score - par) + " over par";

        }

 

        static int GetWinner(int[,] scores, int round)

        {

            int a = 5;

            int best = 0;

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

                if (best == 0 || scores[i, round] < best)

                {

                    a = i;

                    best = scores[i, round];

                }

                else if (scores[i, round] == best)

                {

                    a = 5;

                }

            return a;

        }

    }

}