Skip to content

Instantly share code, notes, and snippets.

@basp1
Last active September 18, 2017 10:48
Show Gist options
  • Save basp1/bfde4ea52fafac2737de59340946c51b to your computer and use it in GitHub Desktop.
Save basp1/bfde4ea52fafac2737de59340946c51b to your computer and use it in GitHub Desktop.
Яндекс.Блиц. C
// https://contest.yandex.ru/hiring/contest/5048/problems/C/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace yandex.blitz
{
public class C_Tournament
{
public enum Game_Result { WIN = 2, TIE = 1, LOSS = 0 };
public class Total
{
string name;
Dictionary<string, Game_Result> results;
int sum;
int position;
public Total()
{
this.results = new Dictionary<string, Game_Result>();
this.sum = 0;
this.position = 0;
}
public void Add(string player, Game_Result result)
{
results[player] = result;
sum += (int)result;
}
public int Sum { get { return sum; } }
public int Position { get { return position; } set { this.position = value; } }
public Dictionary<string, Game_Result> Results { get { return results; } }
}
public static int CountDigits(double value)
{
return (int)Math.Floor(Math.Log10(value) + 1);
}
public static void PrintTableLine(System.IO.StreamWriter file, List<int> lineswidth)
{
foreach (int width in lineswidth)
{
file.Write("+");
for (int i = 0; i < width; i++)
{
file.Write("-");
}
}
file.WriteLine("+");
}
public static void Main(string[] args)
{
var results = LoadData();
var sortedResults = results.Select(kv => kv.Value).OrderByDescending(tot => tot.Sum).ToList();
for (int i = 0, p = 1; p < 4 && i < sortedResults.Count; i++)
{
if (i > 0 && sortedResults[i].Sum != sortedResults[i - 1].Sum)
{
p++;
}
if(4 == p)
{
break;
}
sortedResults[i].Position = p;
}
var sortedNames = results.Keys.OrderBy(name => name).ToList();
var linesWidth = new List<int>();
linesWidth.Add(CountDigits(sortedNames.Count));
linesWidth.Add(sortedNames.Select(name => name.Length).Max());
for (int i = 0; i < sortedNames.Count; i++)
{
linesWidth.Add(1);
}
linesWidth.Add(CountDigits(results.Select(kv => kv.Value.Sum).Max()));
linesWidth.Add(1);
using (var file = new System.IO.StreamWriter("output.txt"))
{
PrintTableLine(file, linesWidth);
for (int i = 0; i < sortedNames.Count; i++)
{
var name = sortedNames[i];
var total = results[name];
file.Write(String.Format("|{0," + linesWidth[0] + "}|{1,-" + linesWidth[1] + "}", (i + 1), name));
for (int j = 0; j < sortedNames.Count; j++)
{
var name2 = sortedNames[j];
if (name == name2)
{
file.Write("|X");
}
else if (!total.Results.ContainsKey(name2))
{
file.Write("| ");
}
else
{
if (Game_Result.WIN == total.Results[name2])
{
file.Write("|W");
}
else if (Game_Result.LOSS == total.Results[name2])
{
file.Write("|L");
}
else
{
file.Write("|D");
}
}
}
file.Write(String.Format("|{0," + linesWidth[linesWidth.Count - 2] + "}", total.Sum));
if (total.Position > 0)
{
file.Write("|" + total.Position);
}
else
{
file.Write("| ");
}
file.WriteLine("|");
PrintTableLine(file, linesWidth);
}
}
}
static Dictionary<string, Total> LoadData()
{
var results = new Dictionary<string, Total>();
using (var file = new System.IO.StreamReader("input.txt"))
{
while (!file.EndOfStream)
{
var line = file.ReadLine();
var values = line.Split('-');
var player = values[0];
if (!results.ContainsKey(player))
{
results[player] = new Total();
}
var player2 = values[1].TrimStart();
if (!results.ContainsKey(player2))
{
results[player2] = new Total();
}
if (" 1:0" == values[2])
{
results[player].Add(player2, Game_Result.WIN);
results[player2].Add(player, Game_Result.LOSS);
}
else if (" 0:1" == values[2])
{
results[player].Add(player2, Game_Result.LOSS);
results[player2].Add(player, Game_Result.WIN);
}
else
{
results[player].Add(player2, Game_Result.TIE);
results[player2].Add(player, Game_Result.TIE);
}
}
}
return results;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment